Skip to content

Commit 089eec5

Browse files
committed
199/208
1 parent 4387490 commit 089eec5

10 files changed

+149
-48
lines changed

README.md

Lines changed: 4 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 196/205 problems.
5+
Solved 199/208 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)|[basic-calculator.cc](basic-calculator.cc)|
16+
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[rectangle-area.cc](rectangle-area.cc)|
17+
|222|[Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)|[count-complete-tree-nodes.cc](count-complete-tree-nodes.cc)|
1518
|221|[Maximal Square](https://leetcode.com/problems/maximal-square/)|[maximal-square.cc](maximal-square.cc)|
1619
|220|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)|[contains-duplicate-iii.cc](contains-duplicate-iii.cc)|
1720
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[contains-duplicate-ii.cc](contains-duplicate-ii.cc)|

basic-calculator.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Basic Calculator
2+
// operator-precedence parser
3+
class Solution
4+
{
5+
public:
6+
int calculate(string s) {
7+
stack<int> xs;
8+
stack<char> ops;
9+
map<char, int> isp, icp;
10+
isp['\0'] = 0; isp['+'] = 3; isp['-'] = 3; isp['('] = 1;
11+
icp['\0'] = 0; icp['+'] = 2; icp['-'] = 2; icp['('] = 4; icp[')'] = 1;
12+
ops.push('\0');
13+
for (size_t i = 0; i <= s.size(); ) {
14+
char op = i == s.size() ? '\0' : s[i];
15+
if (op == ' ')
16+
i++;
17+
else if (isdigit(op)) {
18+
int x = 0;
19+
do x = x*10+s[i]-'0';
20+
while (isdigit(s[++i]));
21+
xs.push(x); // shift
22+
} else {
23+
i++;
24+
int x, y;
25+
for (; isp[ops.top()] > icp[op]; ops.pop()) {
26+
y = xs.top(); xs.pop();
27+
x = xs.top(); xs.pop();
28+
xs.push(ops.top() == '+' ? x+y : x-y); // reduce, X '+' Y => X+Y or X '-' Y => X-Y
29+
}
30+
if (isp[ops.top()] == icp[op]) // reduce, '(' X ')' => X or '\0' X '\0' => X
31+
ops.pop();
32+
else
33+
ops.push(op); // shift
34+
}
35+
}
36+
return xs.top();
37+
}
38+
};

count-complete-tree-nodes.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Count Complete Tree Nodes
2+
class Solution {
3+
public:
4+
int countNodes(TreeNode* root) {
5+
if (! root) return 0;
6+
int dl = 0, dr = 0;
7+
TreeNode *l = root, *r = root;
8+
while (l) l = l->left, dl++;
9+
while (r) r = r->right, dr++;
10+
return dl == dr ? (1 << dl) - 1 : countNodes(root->left) + countNodes(root->right) + 1;
11+
}
12+
};

evaluate-reverse-polish-notation.cc

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
// Evaluate Reverse Polish Notation
22
class Solution {
33
public:
4-
int evalRPN(vector<string> &tokens) {
5-
stack<int> st;
6-
int x, y;
7-
for (auto &s: tokens)
8-
if ('0' <= s[0] && s[0] <= '9' || s[0] == '-' && s.size() > 1)
9-
st.push(atoi(s.c_str()));
10-
else {
11-
y = st.top(); st.pop();
12-
x = st.top(); st.pop();
13-
switch (s[0]) {
14-
case '+':
15-
st.push(x+y);
16-
break;
17-
case '-':
18-
st.push(x-y);
19-
break;
20-
case '*':
21-
st.push(x*y);
22-
break;
23-
case '/':
24-
st.push(x/y);
25-
break;
26-
}
4+
int evalRPN(vector<string> &tokens) {
5+
stack<int> st;
6+
int x, y;
7+
for (auto &s: tokens)
8+
if ('0' <= s[0] && s[0] <= '9' || s[0] == '-' && s.size() > 1)
9+
st.push(atoi(s.c_str()));
10+
else {
11+
y = st.top(); st.pop();
12+
x = st.top(); st.pop();
13+
switch (s[0]) {
14+
case '+':
15+
st.push(x+y);
16+
break;
17+
case '-':
18+
st.push(x-y);
19+
break;
20+
case '*':
21+
st.push(x*y);
22+
break;
23+
case '/':
24+
st.push(x/y);
25+
break;
2726
}
28-
return st.top();
29-
}
27+
}
28+
return st.top();
29+
}
3030
};

palindrome-partitioning-ii.cc

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
// Palindrome Partitioning II
22
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3-
#define ROF(i, a, b) for (int i = (b); --i >= (a); )
3+
#define REP(i, n) FOR(i, 0, n)
44

55
class Solution {
66
public:
77
int minCut(string s) {
8-
if (s.empty()) return 0;
98
int n = s.size();
10-
vector<vector<bool>> f(n, vector<bool>(n));
11-
vector<int> g(n+1);
12-
g[n] = 0;
13-
ROF(i, 0, n) {
14-
f[i][i] = true;
15-
if (i+1 < n)
16-
f[i][i+1] = s[i] == s[i+1];
17-
FOR(j, i+2, n)
18-
if (f[i+1][j-1] && s[i] == s[j])
19-
f[i][j] = true;
20-
g[i] = n;
21-
FOR(j, i, n)
22-
if (f[i][j])
23-
g[i] = min(g[i], g[j+1]+1);
9+
vector<int> f(n+1);
10+
iota(f.begin(), f.end(), -1);
11+
REP(i, n) {
12+
for (int j = 0; i-j >= 0 && i+j < n && s[i-j] == s[i+j]; j++)
13+
f[i+j+1] = min(f[i+j+1], f[i-j]+1);
14+
for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++)
15+
f[i+j+1] = min(f[i+j+1], f[i-j+1]+1);
2416
}
25-
return g[0]-1;
17+
return f[n];
2618
}
2719
};
28-

rectangle-area.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Rectangle Area
2+
// 存在給出的兩個頂點不是左下和右上的測試資料:-2, -2, 2, 2, -3, 3, -4, 4
3+
class Solution {
4+
public:
5+
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
6+
if (A > C) swap(A, C);
7+
if (B > D) swap(B, D);
8+
if (E > G) swap(E, G);
9+
if (F > H) swap(F, H);
10+
int x = max(A, E), y = max(B, F), xx = min(C, G), yy = min(D, H);
11+
return (C-A)*(D-B) + (G-E)*(H-F) - max(xx-x, 0) * max(yy-y, 0);
12+
}
13+
};

repeated-dna-sequences.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
// Repeated DNA Sequences
2+
3+
// rolling hash
4+
5+
class Solution {
6+
public:
7+
vector<string> findRepeatedDnaSequences(string a) {
8+
unsigned i = 0, n = a.size(), t = 0;
9+
unordered_map<unsigned, unsigned> m;
10+
vector<string> r;
11+
while (i < min(9u, n))
12+
t = t<<3 | a[i++]&7;
13+
while (i < n)
14+
if (m[t = t<<3 & 0x3fffffff | a[i++]&7]++ == 1)
15+
r.push_back(a.substr(i-10, 10));
16+
return r;
17+
}
18+
};
19+
20+
// suffix array
21+
222
#define REP(i, n) FOR(i, 0, n)
323
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
424
#define ROF(i, a, b) for (int i = (b); --i >= (a); )

shell.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Tenth Line
2+
3+
```bash
4+
sed -n 10p file.txt
5+
```
6+
7+
## Transpose File
8+
9+
```bash
10+
awk '{for(i=1;i<=NF;i++) {if(s[i]=="") s[i]=$i; else s[i]=s[i]" "$i}} END {for(i=1;s[i]!="";i++) print s[i]; }' file.txt
11+
```
12+
13+
## Valid Phone Numbers
14+
15+
```bash
16+
grep -P '^(\d{3}-|\(\d{3}\) )\d{3}-\d{4}$' file.txt
17+
```
18+
19+
## Word Frequency
20+
21+
```bash
22+
tr -s ' ' '\n' < words.txt | sort | uniq -c | sort -nr | awk '{print $2" "$1}'
23+
```

sort-list.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Sort List
2+
3+
// natural merge sort
4+
25
class Solution {
36
public:
47
ListNode *sortList(ListNode *head) {

two-sum.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
class Solution {
33
public:
44
vector<int> twoSum(vector<int> &a, int s) {
5-
size_t i = 0, j = 1;
65
vector<int> r(a.size());
76
iota(r.begin(), r.end(), 0);
87
sort(r.begin(), r.end(), [&](int x, int y) { return a[x] < a[y]; });
9-
while (j+1 < a.size() && a[r[i]]+a[r[j+1]] <= s) j++;
10-
for (; i < j; i++) {
11-
while (j > 0 && a[r[i]]+a[r[j]] > s) j--;
8+
for (size_t i = 0, j = a.size()-1; i < j; i++) {
9+
while (j > i+1 && a[r[i]]+a[r[j]] > s) j--;
1210
if (a[r[i]]+a[r[j]] == s) {
1311
int x = r[i], y = r[j];
1412
r.clear();

0 commit comments

Comments
 (0)