Skip to content

Commit b578185

Browse files
committed
217/239
1 parent 8ab9592 commit b578185

7 files changed

+170
-2
lines changed

README.md

+21-2
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 211/220 problems.
5+
Solved 217/239 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|255|[Verify Preorder Sequence in Binary Search Tree](/problems/verify-preorder-sequence-in-binary-search-tree/)|[verify-preorder-sequence-in-binary-search-tree.cc](verify-preorder-sequence-in-binary-search-tree.cc)|
16+
|254|[Factor Combinations](/problems/factor-combinations/)|[factor-combinations.cc](factor-combinations.cc)|
17+
|253|[Meeting Rooms II](/problems/meeting-rooms-ii/)|[meeting-rooms-ii.cc](meeting-rooms-ii.cc)|
18+
|252|[Meeting Rooms](/problems/meeting-rooms/)|[meeting-rooms.cc](meeting-rooms.cc)|
19+
|251|[Flatten 2D Vector](/problems/flatten-2d-vector/)|[flatten-2d-vector.cc](flatten-2d-vector.cc)|
20+
|250|[Count Univalue Subtrees](/problems/count-univalue-subtrees/)|[count-univalue-subtrees.cc](count-univalue-subtrees.cc)|
21+
|249|[Group Shifted Strings](/problems/group-shifted-strings/)|[group-shifted-strings.cc](group-shifted-strings.cc)|
22+
|248|[Strobogrammatic Number III](/problems/strobogrammatic-number-iii/)|[strobogrammatic-number-iii.cc](strobogrammatic-number-iii.cc)|
23+
|247|[Strobogrammatic Number II](/problems/strobogrammatic-number-ii/)|[strobogrammatic-number-ii.cc](strobogrammatic-number-ii.cc)|
24+
|246|[Strobogrammatic Number](/problems/strobogrammatic-number/)|[strobogrammatic-number.cc](strobogrammatic-number.cc)|
25+
|245|[Shortest Word Distance III](/problems/shortest-word-distance-iii/)|[shortest-word-distance-iii.cc](shortest-word-distance-iii.cc)|
26+
|244|[Shortest Word Distance II](/problems/shortest-word-distance-ii/)|[shortest-word-distance-ii.cc](shortest-word-distance-ii.cc)|
27+
|243|[Shortest Word Distance](/problems/shortest-word-distance/)|[shortest-word-distance.cc](shortest-word-distance.cc)|
28+
|242|[Valid Anagram](/problems/valid-anagram/)|[valid-anagram.cc](valid-anagram.cc)|
29+
|241|[Different Ways to Add Parentheses](/problems/different-ways-to-add-parentheses/)|[different-ways-to-add-parentheses.cc](different-ways-to-add-parentheses.cc)|
30+
|240|[Search a 2D Matrix II](/problems/search-a-2d-matrix-ii/)|[search-a-2d-matrix-ii.cc](search-a-2d-matrix-ii.cc)|
31+
|239|[Sliding Window Maximum](/problems/sliding-window-maximum/)|[sliding-window-maximum.cc](sliding-window-maximum.cc)|
32+
|238|[Product of Array Except Self](/problems/product-of-array-except-self/)|[product-of-array-except-self.cc](product-of-array-except-self.cc)|
33+
|237|[Delete Node in a Linked List](/problems/delete-node-in-a-linked-list/)|[delete-node-in-a-linked-list.cc](delete-node-in-a-linked-list.cc)|
1534
|236|[Lowest Common Ancestor of a Binary Tree](/problems/lowest-common-ancestor-of-a-binary-tree/)|[lowest-common-ancestor-of-a-binary-tree.cc](lowest-common-ancestor-of-a-binary-tree.cc)|
1635
|235|[Lowest Common Ancestor of a Binary Search Tree](/problems/lowest-common-ancestor-of-a-binary-search-tree/)|[lowest-common-ancestor-of-a-binary-search-tree.cc](lowest-common-ancestor-of-a-binary-search-tree.cc)|
1736
|234|[Palindrome Linked List](/problems/palindrome-linked-list/)|[palindrome-linked-list.cc](palindrome-linked-list.cc)|
@@ -183,7 +202,7 @@ See [database.md](database.md)
183202
|52|[N-Queens II](/problems/n-queens-ii/)|[n-queens-ii.cc](n-queens-ii.cc)|
184203
|51|[N-Queens](/problems/n-queens/)|[n-queens.cc](n-queens.cc)|
185204
|50|[Pow(x, n)](/problems/powx-n/)|[powx-n.cc](powx-n.cc)|
186-
|49|[Anagrams](/problems/anagrams/)|[anagrams.cc](anagrams.cc)|
205+
|49|[Group Anagrams](/problems/anagrams/)|[anagrams.cc](anagrams.cc)|
187206
|48|[Rotate Image](/problems/rotate-image/)|[rotate-image.cc](rotate-image.cc)|
188207
|47|[Permutations II](/problems/permutations-ii/)|[permutations-ii.cc](permutations-ii.cc)|
189208
|46|[Permutations](/problems/permutations/)|[permutations.cc](permutations.cc)|

delete-node-in-a-linked-list.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Delete Node in a Linked List
2+
class Solution {
3+
public:
4+
void deleteNode(ListNode *x) {
5+
auto y = x->next;
6+
swap(x->val, y->val);
7+
x->next = y->next;
8+
delete y;
9+
}
10+
};
11+
12+
//
13+
14+
class Solution {
15+
public:
16+
void deleteNode(ListNode* x) {
17+
ListNode *y;
18+
for (; x->next; y = x, x = x->next)
19+
x->val = x->next->val;
20+
y->next = 0;
21+
}
22+
};

different-ways-to-add-parentheses.cc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Different Ways to Add Parentheses
2+
#define REP(i, n) FOR(i, 0, n)
3+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
4+
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
5+
6+
class Solution {
7+
public:
8+
vector<int> diffWaysToCompute(string input) {
9+
vector<int> nums;
10+
vector<char> ops;
11+
for (int j = 0, i = 0; i < input.size(); i = j) {
12+
int x = 0;
13+
for (; j < input.size() && isdigit(input[j]); j++)
14+
x = x*10+input[j]-'0';
15+
nums.push_back(x);
16+
if (j < input.size())
17+
ops.push_back(input[j++]);
18+
}
19+
int n = nums.size();
20+
vector<vector<vector<int>>> s(n, vector<vector<int>>(n));
21+
REP(i, n)
22+
s[i][i].push_back(nums[i]);
23+
ROF(i, 0, n)
24+
FOR(j, i+1, n)
25+
FOR(k, i, j)
26+
for (auto x: s[i][k])
27+
for (auto y: s[k+1][j])
28+
s[i][j].push_back(op(ops[k], x, y));
29+
return s[0][n-1];
30+
}
31+
int op(char c, int x, int y) {
32+
switch (c) {
33+
case '+': return x+y;
34+
case '-': return x-y;
35+
default: return x*y;
36+
}
37+
}
38+
};

product-of-array-except-self.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Product of Array Except Self
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
5+
6+
class Solution {
7+
public:
8+
vector<int> productExceptSelf(vector<int> &a) {
9+
int n = (int)a.size(), s = 1;
10+
vector<int> b(n);
11+
REP(i, n) {
12+
b[i] = s;
13+
s *= a[i];
14+
}
15+
s = 1;
16+
ROF(i, 0, n) {
17+
b[i] *= s;
18+
s *= a[i];
19+
}
20+
return b;
21+
}
22+
};

search-a-2d-matrix-ii.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Search a 2D Matrix II
2+
class Solution {
3+
public:
4+
bool searchMatrix(vector<vector<int>> &a, int x) {
5+
if (a.empty() || a[0].empty()) return false;
6+
return f(a, 0, a.size(), 0, a[0].size(), x);
7+
}
8+
bool f(vector<vector<int>> &a, int u, int uu, int v, int vv, int x) {
9+
if (u >= uu || v >= vv) return false;
10+
if (uu-u > vv-v) {
11+
int q = (v+vv)/2, l = u, h = uu;
12+
while (l < h) {
13+
int m = (l+h)/2;
14+
if (a[m][q] < x) l = m+1;
15+
else h = m;
16+
}
17+
if (l < uu && a[l][q] == x) return true;
18+
return f(a, u, l, q+1, vv, x) || f(a, l, uu, v, q, x);
19+
} else {
20+
int p = (u+uu)/2, l = v, h = vv;
21+
while (l < h) {
22+
int m = (l+h)/2;
23+
if (a[p][m] < x) l = m+1;
24+
else h = m;
25+
}
26+
if (l < vv && a[p][l] == x) return true;
27+
return f(a, p+1, uu, v, l, x) || f(a, u, p, l, vv, x);
28+
}
29+
}
30+
};

sliding-window-maximum.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Sliding Window Maximum
2+
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
5+
class Solution {
6+
public:
7+
vector<int> maxSlidingWindow(vector<int> &a, int k) {
8+
int n = (int)a.size();
9+
vector<int> b;
10+
deque<int> q;
11+
REP(i, n) {
12+
if (q.size() && q.front() <= i-k)
13+
q.pop_front();
14+
while (q.size() && a[q.back()] < a[i])
15+
q.pop_back();
16+
q.push_back(i);
17+
if (i >= k-1)
18+
b.push_back(a[q.front()]);
19+
}
20+
return b;
21+
}
22+
};

valid-anagram.cc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Valid Anagram
2+
class Solution {
3+
public:
4+
bool isAnagram(string s, string t) {
5+
int c[26] = {};
6+
for (auto a: s)
7+
c[a-'a']++;
8+
for (auto a: t)
9+
c[a-'a']--;
10+
for (auto a: c)
11+
if (a)
12+
return false;
13+
return true;
14+
}
15+
};

0 commit comments

Comments
 (0)