Skip to content

Instantly share code, notes, and snippets.

@toliuweijing
toliuweijing / Binary Tree Level Order Traversal.hpp
Created February 17, 2013 22:16
LeetCode: Binary Tree Level Order Traversal
// Idea: BFS from root level-by-level down to leaves
// Pseudo code
// starting from root, put it into a queue Q.
// while Q has elements
// popup E from Q
// put E’s children nodes into Q
#include<queue>
@toliuweijing
toliuweijing / gist:5174734
Last active December 15, 2015 00:39
C++: Lightweight Unitest Framework
I have been looking for unitest frameworks and have tried some of them,
such as the one in Boost. They all look a little bit heavyweight and
take times to learn. So I decided to write one for my own use. After
all, what I write will fit what I need.
What I expect on a unitest framework depends on the way I use it.
Basically, I apply unitest on functions. Everytime I add a new function,
I write a corresponding unitest. Pure functions are easy to test. All
we need is to feed an input and verify the output. Functions that
work by side effects are tricky to test. Especially when it is a member
@toliuweijing
toliuweijing / Vertical-Splitting Screen on Mac
Created April 19, 2013 22:23
Just found a handyful instruction for getting gnu screen on mac that supports vertical splitting.
http://old.evanmeagher.net/2010/12/patching-screen-with-vertical-split-in-os
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int A[256] = {0};
int len = 0;
for (int i = 0, j = 0 ; j < s.size() ; ++j) {
if (A[s[j]]++ == 0) {
len = max(len, j-i+1);
}
else {
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
return helper(l1, l2, 0);
}
ListNode* helper(ListNode* l1, ListNode* l2, int carry) {
if (l1 == l2 && l1 == 0 && carry == 0) return 0;
int a = l1 ? l1->val : 0;
class Solution {
public:
string longestPalindrome(string s) {
int opti = 0;
int optj = 0;
for (int k = 0 ; k < s.size() ; k++) {
for (int off = 0 ; off <= 1 ; ++off) {
int i = k;
int j = k+off;
class Solution {
public:
string convert(string s, int nRows) {//pay
if (nRows == 1) return s;
vector<string> A(nRows);
int idx = 0;
int dir = 1;
for (auto ch : s) {
class Solution {
public:
int reverse(int x) {
int ret = 0;
while (x != 0) {
ret *= 10;
ret += x % 10;
x /= 10;
}
return ret;
class Solution {
public:
bool isPalindrome(int x) {
int old = abs(x);
int cur = 0;
while (x != 0) {
cur *= 10;
cur += x % 10;
x /= 10;
}
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (*p == '\0') return *s == '\0';
if (*s && (*p == *s || *p == '.') && isMatch(s+1, p+1)) return true;
if (*(p+1) == '*') {
if (isMatch(s, p+2)) return true;
while (*s && (*p == *s || *p == '.')) {