Skip to content

Commit 7daaf9e

Browse files
committedJul 6, 2015
lots of update
1 parent 9f7407f commit 7daaf9e

11 files changed

+187
-25
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[LeetCode solutions](http://maskray.me/blog/2014-06-29-leetcode-solutions) gives some thoughts on selected problems.
44

5-
Solved 204/213 problems.
5+
Solved 206/215 problems.
66

77
## Database
88

@@ -12,6 +12,8 @@ See [database.md](database.md)
1212

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|231|[Power of Two](/problems/power-of-two/)|[power-of-two.cc](power-of-two.cc)|
16+
|230|[Kth Smallest Element in a BST](/problems/kth-smallest-element-in-a-bst/)|[kth-smallest-element-in-a-bst.cc](kth-smallest-element-in-a-bst.cc)|
1517
|229|[Majority Element II](/problems/majority-element-ii/)|[majority-element-ii.cc](majority-element-ii.cc)|
1618
|228|[Summary Ranges](/problems/summary-ranges/)|[summary-ranges.cc](summary-ranges.cc)|
1719
|227|[Basic Calculator II](/problems/basic-calculator-ii/)|[basic-calculator-ii.cc](basic-calculator-ii.cc)|

‎construct-binary-tree-from-inorder-and-postorder-traversal.cc

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
// Construct Binary Tree from Inorder and Postorder Traversal
2+
3+
// iterative
4+
5+
class Solution {
6+
public:
7+
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
8+
if (inorder.empty()) return nullptr;
9+
stack<TreeNode *> s;
10+
TreeNode *x = nullptr;
11+
auto i = inorder.begin(), j = postorder.begin();
12+
while (j != postorder.end())
13+
if (! s.empty() && s.top()->val == *j) {
14+
s.top()->right = x;
15+
x = s.top();
16+
s.pop();
17+
++j;
18+
} else {
19+
auto y = new TreeNode(*i++);
20+
y->left = x;
21+
x = nullptr;
22+
s.push(y);
23+
}
24+
return x;
25+
}
26+
};
27+
28+
// recursive
29+
230
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
331
#define REP(i, n) FOR(i, 0, n)
432

@@ -8,8 +36,7 @@ class Solution {
836
return f(&postorder[0], &*postorder.end(), &inorder[0], &*inorder.end());
937
}
1038
TreeNode *f(int *l, int *h, int *ll, int *hh) {
11-
if (l == h)
12-
return NULL;
39+
if (l == h) return nullptr;
1340
auto r = new TreeNode(h[-1]);
1441
int *m = find(ll, hh, h[-1]);
1542
r->left = f(l, l+(m-ll), ll, m);

‎construct-binary-tree-from-preorder-and-inorder-traversal.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
// Construct Binary Tree from Preorder and Inorder Traversal
2+
3+
// iterative
4+
class Solution {
5+
public:
6+
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
7+
if (preorder.empty()) return nullptr;
8+
auto i = preorder.begin(), j = inorder.begin();
9+
auto root = new TreeNode(*i++);
10+
stack<TreeNode *> s;
11+
s.push(root);
12+
while (i != preorder.end()) {
13+
auto x = s.top();
14+
if (x->val != *j) {
15+
x->left = new TreeNode(*i++);
16+
x = x->left;
17+
s.push(x);
18+
} else {
19+
s.pop();
20+
++j;
21+
if (s.empty() || s.top()->val != *j) {
22+
x->right = new TreeNode(*i++);
23+
x = x->right;
24+
s.push(x);
25+
}
26+
}
27+
}
28+
return root;
29+
}
30+
};
31+
32+
// recursive
33+
234
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
335
#define REP(i, n) FOR(i, 0, n)
436

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// Convert Sorted List to Binary Search Tree
22
class Solution {
3+
ListNode *l;
4+
TreeNode *f(int n) {
5+
if (! n) return 0;
6+
auto x = new TreeNode(0);
7+
x->left = f(n/2);
8+
x->val = l->val;
9+
l = l->next;
10+
x->right = f(n-n/2-1);
11+
return x;
12+
}
313
public:
414
TreeNode *sortedListToBST(ListNode *head) {
5-
if (! head) return NULL;
6-
ListNode *p = head, *q = head, *r = NULL;
7-
while (q->next && q->next->next) {
8-
r = p;
9-
p = p->next;
10-
q = q->next->next;
15+
l = head;
16+
int n = 0;
17+
while (head) {
18+
head = head->next;
19+
n++;
1120
}
12-
TreeNode *x = new TreeNode(p->val);
13-
if (r) {
14-
r->next = NULL;
15-
x->left = sortedListToBST(head);
16-
}
17-
x->right = sortedListToBST(p->next);
18-
return x;
21+
return f(n);
1922
}
2023
};

‎find-minimum-in-rotated-sorted-array-ii.cc

+19
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,22 @@ class Solution {
1717
return a[l];
1818
}
1919
};
20+
21+
//
22+
23+
class Solution {
24+
public:
25+
int findMin(vector<int> &a) {
26+
int l = 0, h = a.size()-1;
27+
while (l < h) {
28+
int m = l+h >> 1;
29+
if (a[m] > a[h])
30+
l = m+1;
31+
else if (a[m] < a[h])
32+
h = m;
33+
else
34+
h--;
35+
}
36+
return a[l];
37+
}
38+
};

‎kth-smallest-element-in-a-bst.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Kth Smallest Element in a BST
2+
class Solution {
3+
public:
4+
int kthSmallest(TreeNode *x, int k) {
5+
vector<TreeNode *> s(k);
6+
int i = 0, j = k;
7+
for(;;) {
8+
while (x) {
9+
s[i++%k] = x;
10+
x = x->left;
11+
}
12+
x = s[--i%k];
13+
if (! --j)
14+
return x->val;
15+
x = x->right;
16+
}
17+
}
18+
};

‎length-of-last-word.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Length of Last Word
22
class Solution {
33
public:
4-
int lengthOfLastWord(const char *s) {
5-
const char *p = s+strlen(s), *q;
6-
while (s < p && p[-1] == ' ') p--;
7-
q = p;
8-
while (s < p && p[-1] != ' ') p--;
9-
return q-p;
4+
int lengthOfLastWord(string a) {
5+
const char *s = a.c_str();
6+
int l = 0;
7+
while (*s) {
8+
if (*s++ != ' ')
9+
l++;
10+
else if (*s && *s != ' ')
11+
l = 0;
12+
}
13+
return l;
1014
}
1115
};

‎longest-substring-without-repeating-characters.cc

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
// Longest Substring Without Repeating Characters
2+
class Solution {
3+
public:
4+
int lengthOfLongestSubstring(string s) {
5+
vector<int> c(127, -1);
6+
int m = -1, r = 0;
7+
for (int i = 0; i < s.size(); i++) {
8+
m = max(m, c[s[i]]);
9+
r = max(r, i-m);
10+
c[s[i]] = i;
11+
}
12+
return r;
13+
}
14+
};
15+
16+
//
17+
218
class Solution {
319
public:
420
int lengthOfLongestSubstring(string s) {

‎power-of-two.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Power of Two
2+
class Solution {
3+
public:
4+
bool isPowerOfTwo(int n) {
5+
return n > 0 && !(n & n-1);
6+
}
7+
};

‎single-number-ii.cc

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
// Single Number II
2+
23
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
34
#define REP(i, n) FOR(i, 0, n)
45
class Solution {
56
public:
6-
int singleNumber(int A[], int n) {
7+
int singleNumber(vector<int> &a) {
78
vector<int> c(32);
8-
REP(i, n)
9+
for (int x: a)
910
REP(j, 32)
10-
c[j] += A[i]>>j&1;
11+
c[j] += x>>j & 1;
1112
int r = 0;
1213
REP(j, 32)
1314
r += c[j]%3 << j;
1415
return r;
1516
}
1617
};
18+
19+
//
20+
21+
class Solution {
22+
public:
23+
int singleNumber(vector<int> &a) {
24+
int one = 0, two = 0;
25+
for (int x: a) {
26+
one = (one ^ x) & ~ two;
27+
two = (two ^ x) & ~ one;
28+
}
29+
return one;
30+
}
31+
};

‎valid-sudoku.cc

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
// Valid Sudoku
22
#define REP(i, n) for (int i = 0; i < (n); i++)
33

4+
class Solution {
5+
public:
6+
bool isValidSudoku(vector<vector<char> > &a) {
7+
int8_t r[9][9] = {}, c[9][9] = {}, b[9][9] = {};
8+
REP(i, 9)
9+
REP(j, 9)
10+
if (a[i][j] != '.') {
11+
int x = a[i][j]-'1';
12+
if (r[i][x]++ || c[j][x]++ || b[i/3*3+j/3][x]++)
13+
return false;
14+
}
15+
return true;
16+
}
17+
};
18+
19+
// less stack space, but longer code
20+
21+
#define REP(i, n) for (int i = 0; i < (n); i++)
22+
423
class Solution {
524
public:
625
bool isValidSudoku(vector<vector<char> > &a) {

0 commit comments

Comments
 (0)
Please sign in to comment.