Skip to content

Instantly share code, notes, and snippets.

@ywei89
ywei89 / netstat_aggregation.sh
Created March 7, 2014 08:44
有``netstat -an``的输出,统计输出连接到本主机连接数最多的10个IP,并按连接数从多到少排序
awk '{cnt[$5] += 1;} END {for (ip in cnt) {print cnt[ip], ip;}}' netstat_output.txt | sort -nr | head -n 10
@ywei89
ywei89 / catline.sh
Created March 7, 2014 03:20
将偶数行附加在奇数行后,并用空格分开
#!/bin/awk -f
{
if (NR % 2 == 1) {
ORS = " ";
} else {
ORS = "\n";
}
print ;
}
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> output;
string cur = words.front();
string feed;
vector<string> line;
line.push_back(cur);
int gross = cur.length();
@ywei89
ywei89 / gas_station.cpp
Created March 4, 2014 02:51
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas station's index if you can travel ar…
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int N = gas.size(); // assert gas.size() == cost.size()
vector<int> diff(N, 0);
for (int i = 0; i < N; i++) {
diff[i] = gas[i] - cost[i];
}
int i = 0;
@ywei89
ywei89 / rotate_list.cpp
Created February 27, 2014 05:41
Given a list, rotate the list to the right by k places, where k is non-negative.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@ywei89
ywei89 / copy_list_random.cpp
Created February 27, 2014 03:42
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
@ywei89
ywei89 / reorder_list.cpp
Created February 26, 2014 09:00
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@ywei89
ywei89 / merge_k_sorted_lists.cpp
Created February 26, 2014 07:43
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@ywei89
ywei89 / sort_list.cpp
Last active August 29, 2015 13:56 — forked from anonymous/sort_list.cpp
Sort a linked list in O(n log n) time using constant space complexity.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@ywei89
ywei89 / permutations_2.cpp
Created February 24, 2014 07:50
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > res;
vector<int> init(num);
sort(init.begin(), init.end());
res.push_back(init);
int p, q;
while (true) {