Skip to content

Commit aaf4ca8

Browse files
committed
add 39 locked, solved 273/273
1 parent eb6f0f1 commit aaf4ca8

40 files changed

+905
-1
lines changed

3sum-smaller.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 3Sum Smaller
2+
class Solution {
3+
public:
4+
int threeSumSmaller(vector<int>& a, int target) {
5+
int n = a.size(), r = 0;
6+
sort(a.begin(), a.end());
7+
for (int i = 0; i < n; i++) {
8+
int j = i+1, k = n-1;
9+
for (; j < n; j++) {
10+
for (; j < k && a[i]+a[j]+a[k] >= target; k--);
11+
if (j < k)
12+
r += k-j;
13+
}
14+
}
15+
return r;
16+
}
17+
};

README.md

Lines changed: 40 additions & 1 deletion
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 234/273 problems.
5+
Solved 273/273 problems.
66

77
## Database
88

@@ -14,21 +14,51 @@ See [database.md](database.md)
1414
|---| ----- | -------- |
1515
|290|[Word Pattern](https://leetcode.com/problems/word-pattern/)|[word-pattern.cc](word-pattern.cc)|
1616
|289|[Game of Life](https://leetcode.com/problems/game-of-life/)|[game-of-life.cc](game-of-life.cc)|
17+
|288|[Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/)|[unique-word-abbreviation.cc](unique-word-abbreviation.cc)|
1718
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)|[find-the-duplicate-number.cc](find-the-duplicate-number.cc)|
19+
|286|[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)|[walls-and-gates.cc](walls-and-gates.cc)|
20+
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/)|[inorder-successor-in-bst.cc](inorder-successor-in-bst.cc)|
1821
|284|[Peeking Iterator](https://leetcode.com/problems/peeking-iterator/)|[peeking-iterator.cc](peeking-iterator.cc)|
1922
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/)|[move-zeroes.cc](move-zeroes.cc)|
2023
|282|[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)|[expression-add-operators.cc](expression-add-operators.cc)|
24+
|281|[Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/)|[zigzag-iterator.cc](zigzag-iterator.cc)|
25+
|280|[Wiggle Sort](https://leetcode.com/problems/wiggle-sort/)|[wiggle-sort.cc](wiggle-sort.cc)|
2126
|279|[Perfect Squares](https://leetcode.com/problems/perfect-squares/)|[perfect-squares.cc](perfect-squares.cc)|
2227
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)|[first-bad-version.cc](first-bad-version.cc)|
28+
|277|[Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/)|[find-the-celebrity.cc](find-the-celebrity.cc)|
29+
|276|[Paint Fence](https://leetcode.com/problems/paint-fence/)|[paint-fence.cc](paint-fence.cc)|
2330
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/)|[h-index-ii.cc](h-index-ii.cc)|
2431
|274|[H-Index](https://leetcode.com/problems/h-index/)|[h-index.cc](h-index.cc)|
2532
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)|[integer-to-english-words.cc](integer-to-english-words.cc)|
33+
|272|[Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/)|[closest-binary-search-tree-value-ii.cc](closest-binary-search-tree-value-ii.cc)|
34+
|271|[Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/)|[encode-and-decode-strings.cc](encode-and-decode-strings.cc)|
35+
|270|[Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/)|[closest-binary-search-tree-value.cc](closest-binary-search-tree-value.cc)|
36+
|269|[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)|[alien-dictionary.cc](alien-dictionary.cc)|
2637
|268|[Missing Number](https://leetcode.com/problems/missing-number/)|[missing-number.cc](missing-number.cc)|
38+
|267|[Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/)|[palindrome-permutation-ii.cc](palindrome-permutation-ii.cc)|
39+
|266|[Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/)|[palindrome-permutation.cc](palindrome-permutation.cc)|
40+
|265|[Paint House II](https://leetcode.com/problems/paint-house-ii/)|[paint-house-ii.cc](paint-house-ii.cc)|
2741
|264|[Ugly Number II](https://leetcode.com/problems/ugly-number-ii/)|[ugly-number-ii.cc](ugly-number-ii.cc)|
2842
|263|[Ugly Number](https://leetcode.com/problems/ugly-number/)|[ugly-number.cc](ugly-number.cc)|
43+
|261|[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)|[graph-valid-tree.cc](graph-valid-tree.cc)|
2944
|260|[Single Number III](https://leetcode.com/problems/single-number-iii/)|[single-number-iii.cc](single-number-iii.cc)|
45+
|259|[3Sum Smaller](https://leetcode.com/problems/3sum-smaller/)|[3sum-smaller.cc](3sum-smaller.cc)|
3046
|258|[Add Digits](https://leetcode.com/problems/add-digits/)|[add-digits.cc](add-digits.cc)|
3147
|257|[Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)|[binary-tree-paths.cc](binary-tree-paths.cc)|
48+
|256|[Paint House](https://leetcode.com/problems/paint-house/)|[paint-house.cc](paint-house.cc)|
49+
|255|[Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/)|[verify-preorder-sequence-in-binary-search-tree.cc](verify-preorder-sequence-in-binary-search-tree.cc)|
50+
|254|[Factor Combinations](https://leetcode.com/problems/factor-combinations/)|[factor-combinations.cc](factor-combinations.cc)|
51+
|253|[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)|[meeting-rooms-ii.cc](meeting-rooms-ii.cc)|
52+
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)|[meeting-rooms.cc](meeting-rooms.cc)|
53+
|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)|[flatten-2d-vector.cc](flatten-2d-vector.cc)|
54+
|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)|[count-univalue-subtrees.cc](count-univalue-subtrees.cc)|
55+
|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)|[group-shifted-strings.cc](group-shifted-strings.cc)|
56+
|248|[Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/)|[strobogrammatic-number-iii.cc](strobogrammatic-number-iii.cc)|
57+
|247|[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)|[strobogrammatic-number-ii.cc](strobogrammatic-number-ii.cc)|
58+
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)|[strobogrammatic-number.cc](strobogrammatic-number.cc)|
59+
|245|[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)|[shortest-word-distance-iii.cc](shortest-word-distance-iii.cc)|
60+
|244|[Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/)|[shortest-word-distance-ii.cc](shortest-word-distance-ii.cc)|
61+
|243|[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)|[shortest-word-distance.cc](shortest-word-distance.cc)|
3262
|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/)|[valid-anagram.cc](valid-anagram.cc)|
3363
|241|[Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/)|[different-ways-to-add-parentheses.cc](different-ways-to-add-parentheses.cc)|
3464
|240|[Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/)|[search-a-2d-matrix-ii.cc](search-a-2d-matrix-ii.cc)|
@@ -79,18 +109,27 @@ See [database.md](database.md)
79109
|189|[Rotate Array](https://leetcode.com/problems/rotate-array/)|[rotate-array.cc](rotate-array.cc)|
80110
|188|[Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/)|[best-time-to-buy-and-sell-stock-iv.cc](best-time-to-buy-and-sell-stock-iv.cc)|
81111
|187|[Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/)|[repeated-dna-sequences.cc](repeated-dna-sequences.cc)|
112+
|186|[Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/)|[reverse-words-in-a-string-ii.cc](reverse-words-in-a-string-ii.cc)|
82113
|179|[Largest Number](https://leetcode.com/problems/largest-number/)|[largest-number.cc](largest-number.cc)|
83114
|174|[Dungeon Game](https://leetcode.com/problems/dungeon-game/)|[dungeon-game.cc](dungeon-game.cc)|
84115
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)|[binary-search-tree-iterator.cc](binary-search-tree-iterator.cc)|
85116
|172|[Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/)|[factorial-trailing-zeroes.cc](factorial-trailing-zeroes.cc)|
86117
|171|[Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/)|[excel-sheet-column-number.cc](excel-sheet-column-number.cc)|
118+
|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)|[two-sum-iii-data-structure-design.cc](two-sum-iii-data-structure-design.cc)|
87119
|169|[Majority Element](https://leetcode.com/problems/majority-element/)|[majority-element.cc](majority-element.cc)|
88120
|168|[Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)|[excel-sheet-column-title.cc](excel-sheet-column-title.cc)|
121+
|167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|[two-sum-ii-input-array-is-sorted.cc](two-sum-ii-input-array-is-sorted.cc)|
89122
|166|[Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)|[fraction-to-recurring-decimal.cc](fraction-to-recurring-decimal.cc)|
90123
|165|[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/)|[compare-version-numbers.cc](compare-version-numbers.cc)|
91124
|164|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|[maximum-gap.cc](maximum-gap.cc)|
125+
|163|[Missing Ranges](https://leetcode.com/problems/missing-ranges/)|[missing-ranges.cc](missing-ranges.cc)|
92126
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/)|[find-peak-element.cc](find-peak-element.cc)|
127+
|161|[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)|[one-edit-distance.cc](one-edit-distance.cc)|
93128
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)|[intersection-of-two-linked-lists.cc](intersection-of-two-linked-lists.cc)|
129+
|159|[Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)|[longest-substring-with-at-most-two-distinct-characters.cc](longest-substring-with-at-most-two-distinct-characters.cc)|
130+
|158|[Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/)|[read-n-characters-given-read4-ii-call-multiple-times.cc](read-n-characters-given-read4-ii-call-multiple-times.cc)|
131+
|157|[Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/)|[read-n-characters-given-read4.cc](read-n-characters-given-read4.cc)|
132+
|156|[Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/)|[binary-tree-upside-down.cc](binary-tree-upside-down.cc)|
94133
|155|[Min Stack](https://leetcode.com/problems/min-stack/)|[min-stack.cc](min-stack.cc)|
95134
|154|[Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)|[find-minimum-in-rotated-sorted-array-ii.cc](find-minimum-in-rotated-sorted-array-ii.cc)|
96135
|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)|[find-minimum-in-rotated-sorted-array.cc](find-minimum-in-rotated-sorted-array.cc)|

alien-dictionary.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Alien Dictionary
2+
class Solution {
3+
public:
4+
string alienOrder(vector<string>& words) {
5+
bool f[26] = {};
6+
int d[26] = {}, top = -1, ab = 0;
7+
vector<int> e[26];
8+
for (auto &w: words)
9+
for (auto c: w)
10+
f[c-'a'] = true;
11+
for (int i = 1; i < words.size(); i++) {
12+
string &x = words[i-1], &y = words[i];
13+
int j = 0;
14+
while (x[j] == y[j] && x[j])
15+
j++;
16+
if (x[j] && y[j]) {
17+
e[x[j]-'a'].push_back(y[j]-'a');
18+
d[y[j]-'a']++;
19+
}
20+
}
21+
string r;
22+
for (int i = 0; i < 26; i++)
23+
if (f[i]) {
24+
ab++;
25+
if (! d[i])
26+
d[i] = top, top = i;
27+
}
28+
while (top != -1) {
29+
int x = top;
30+
top = d[x];
31+
r.push_back('a'+x);
32+
for (auto y: e[x])
33+
if (! --d[y])
34+
d[y] = top, top = y;
35+
}
36+
return r.size() < ab ? "" : r;
37+
}
38+
};

binary-tree-upside-down.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Binary Tree Upside Down
2+
class Solution {
3+
public:
4+
TreeNode* upsideDownBinaryTree(TreeNode *x) {
5+
TreeNode *l = NULL, *r = NULL, *y;
6+
while (x) {
7+
y = x;
8+
swap(x->left, l);
9+
swap(x->right, r);
10+
x = l;
11+
l = r;
12+
r = y;
13+
}
14+
return r;
15+
}
16+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Closest Binary Search Tree Value II
2+
class Solution {
3+
public:
4+
vector<int> closestKValues(TreeNode* root, double target, int k) {
5+
stack<TreeNode*> sl, sr;
6+
TreeNode *x = root;
7+
while (x)
8+
if (x->val <= target) {
9+
sl.push(x);
10+
x = x->right;
11+
} else
12+
x = x->left;
13+
x = root;
14+
while (x)
15+
if (x->val > target) {
16+
sr.push(x);
17+
x = x->left;
18+
} else
19+
x = x->right;
20+
auto popl = [&]() {
21+
int v = sl.top()->val;
22+
auto x = sl.top()->left;
23+
sl.pop();
24+
for (; x; x = x->right)
25+
sl.push(x);
26+
return v;
27+
};
28+
auto popr = [&]() {
29+
int v = sr.top()->val;
30+
auto x = sr.top()->right;
31+
sr.pop();
32+
for (; x; x = x->left)
33+
sr.push(x);
34+
return v;
35+
};
36+
vector<int> r;
37+
while (r.size() < k)
38+
r.push_back(sr.empty() || ! sl.empty() && fabs(sl.top()->val-target) < fabs(sr.top()->val-target) ? popl() : popr());
39+
return r;
40+
}
41+
};

closest-binary-search-tree-value.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Closest Binary Search Tree Value
2+
class Solution {
3+
public:
4+
int closestValue(TreeNode *x, double target) {
5+
int opt;
6+
double optd = numeric_limits<double>::max();
7+
while (x) {
8+
double d = fabs(target-x->val);
9+
if (d < optd)
10+
optd = d, opt = x->val;
11+
x = target < x->val ? x->left : x->right;
12+
}
13+
return opt;
14+
}
15+
};

count-univalue-subtrees.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Count Univalue Subtrees
2+
3+
// recursive
4+
class Solution {
5+
bool f(TreeNode *x, int &r) {
6+
if (! x) return true;
7+
auto fl = f(x->left, r),
8+
fr = f(x->right, r);
9+
if (fl && fr && (! x->left || x->left->val == x->val) &&
10+
(! x->right || x->right->val == x->val))
11+
return r++, true;
12+
return false;
13+
}
14+
public:
15+
int countUnivalSubtrees(TreeNode *x) {
16+
int r = 0;
17+
f(x, r);
18+
return r;
19+
}
20+
};
21+
22+
// Morris-like post-order traversal
23+
class Solution {
24+
void reverse_right_chain(TreeNode *x, TreeNode *y) {
25+
TreeNode *z = x->right, *t;
26+
while (x != y) {
27+
t = z->right;
28+
z->right = x;
29+
x = z;
30+
z = t;
31+
}
32+
}
33+
public:
34+
int countUnivalSubtrees(TreeNode *x) {
35+
TreeNode aux(0), *y, *z, *t;
36+
aux.left = x;
37+
x = &aux;
38+
int r = 0;
39+
while (x) {
40+
y = x->left;
41+
if (y) {
42+
while (y->right && y->right != x)
43+
y = y->right;
44+
if (y->right == x) {
45+
reverse_right_chain(x->left, y);
46+
t = NULL;
47+
for (z = y; ; t = z, z = z->right) {
48+
if (z->left && z->left->val != z->val || t && t->val != z->val)
49+
z->val = INT_MIN;
50+
else
51+
r++;
52+
if (z == x->left) break;
53+
}
54+
reverse_right_chain(y, x->left);
55+
y->right = NULL;
56+
} else {
57+
y->right = x;
58+
x = x->left;
59+
continue;
60+
}
61+
}
62+
x = x->right;
63+
}
64+
return r;
65+
}
66+
};

encode-and-decode-strings.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Encode and Decode Strings
2+
class Codec {
3+
public:
4+
5+
// Encodes a list of strings to a single string.
6+
string encode(vector<string>& strs) {
7+
string r;
8+
for (auto &s: strs)
9+
r += to_string(s.size())+':'+s;
10+
return r;
11+
}
12+
13+
// Decodes a single string to a list of strings.
14+
vector<string> decode(string s) {
15+
vector<string> r;
16+
for (int l, j, i = 0; i < s.size(); i = j+1+l) {
17+
j = s.find(':', i);
18+
l = stoi(s.substr(i, j-i), NULL);
19+
r.push_back(s.substr(j+1, l));
20+
}
21+
return r;
22+
}
23+
};

factor-combinations.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Factor Combinations
2+
class Solution {
3+
void f(int n, int k, vector<int> &a, vector<vector<int>> &r) {
4+
for (; k*k <= n; k++)
5+
if (n%k == 0) {
6+
a.push_back(k);
7+
f(n/k, k, a, r);
8+
a.pop_back();
9+
}
10+
if (k <= n) {
11+
a.push_back(n);
12+
f(1, n, a, r);
13+
a.pop_back();
14+
}
15+
}
16+
public:
17+
vector<vector<int>> getFactors(int n) {
18+
vector<int> a;
19+
vector<vector<int>> r;
20+
f(n, 2, a, r);
21+
return r;
22+
}
23+
};

find-the-celebrity.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Find the Celebrity
2+
3+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
4+
#define REP(i, n) FOR(i, 0, n)
5+
6+
// Forward declaration of the knows API.
7+
bool knows(int a, int b);
8+
9+
class Solution {
10+
public:
11+
int findCelebrity(int n) {
12+
int x = 0;
13+
FOR(i, 1, n)
14+
if (knows(x, i))
15+
x = i;
16+
REP(i, x)
17+
if (knows(x, i))
18+
return -1;
19+
REP(i, n)
20+
if (i != x && ! knows(i, x))
21+
return -1;
22+
return x;
23+
}
24+
};

flatten-2d-vector.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Flatten 2D Vector
2+
class Vector2D {
3+
vector<int>::iterator i;
4+
vector<vector<int>>::iterator j, jj;
5+
public:
6+
Vector2D(vector<vector<int>>& vec2d) {
7+
j = vec2d.begin();
8+
jj = vec2d.end();
9+
if (j != jj)
10+
i = j->begin();
11+
}
12+
13+
int next() {
14+
while (i == j->end())
15+
i = (++j)->begin();
16+
return *i++;
17+
}
18+
19+
bool hasNext() {
20+
if (j == jj)
21+
return false;
22+
for(;;) {
23+
if (i != j->end())
24+
return true;
25+
if (++j == jj)
26+
return false;
27+
i = j->begin();
28+
}
29+
}
30+
};

0 commit comments

Comments
 (0)