This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://old.evanmeagher.net/2010/12/patching-screen-with-vertical-split-in-os |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int reverse(int x) { | |
int ret = 0; | |
while (x != 0) { | |
ret *= 10; | |
ret += x % 10; | |
x /= 10; | |
} | |
return ret; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isPalindrome(int x) { | |
int old = abs(x); | |
int cur = 0; | |
while (x != 0) { | |
cur *= 10; | |
cur += x % 10; | |
x /= 10; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 == '.')) { |
OlderNewer