Skip to content

Commit d209927

Browse files
committed
223/252
1 parent e42582d commit d209927

7 files changed

+88
-1
lines changed

README.md

+6-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 217/239 problems.
5+
Solved 222/251 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|268|[Missing Number](/problems/missing-number/)|[missing-number.cc](missing-number.cc)|
16+
|264|[Ugly Number II](/problems/ugly-number-ii/)|[ugly-number-ii.cc](ugly-number-ii.cc)|
17+
|263|[Ugly Number](/problems/ugly-number/)|[ugly-number.cc](ugly-number.cc)|
18+
|258|[Add Digits](/problems/add-digits/)|[add-digits.cc](add-digits.cc)|
19+
|257|[Binary Tree Paths](/problems/binary-tree-paths/)|[binary-tree-paths.cc](binary-tree-paths.cc)|
1520
|242|[Valid Anagram](/problems/valid-anagram/)|[valid-anagram.cc](valid-anagram.cc)|
1621
|241|[Different Ways to Add Parentheses](/problems/different-ways-to-add-parentheses/)|[different-ways-to-add-parentheses.cc](different-ways-to-add-parentheses.cc)|
1722
|240|[Search a 2D Matrix II](/problems/search-a-2d-matrix-ii/)|[search-a-2d-matrix-ii.cc](search-a-2d-matrix-ii.cc)|

add-digits.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Add Digits
2+
class Solution {
3+
public:
4+
int addDigits(int num) {
5+
return num ? num%9 ? num%9 : 9 : 0;
6+
}
7+
};

binary-tree-paths.cc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Binary Tree Paths
2+
class Solution {
3+
vector<string> r;
4+
void f(TreeNode *x, string s) {
5+
if (! x) return;
6+
s = s.empty() ? to_string(x->val) : s+"->"+to_string(x->val);
7+
f(x->left, s);
8+
f(x->right, s);
9+
if (! x->left && ! x->right)
10+
r.push_back(s);
11+
}
12+
public:
13+
vector<string> binaryTreePaths(TreeNode *root) {
14+
f(root, "");
15+
return r;
16+
}
17+
};

missing-number.cc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Missing Number
2+
class Solution {
3+
public:
4+
int missingNumber(vector<int> &a) {
5+
int n = (int)a.size();
6+
for (int i = 0; i < n; ) {
7+
int j = a[i];
8+
if (i != j && 0 <= j && j < n)
9+
swap(a[i], a[j]);
10+
else
11+
i++;
12+
}
13+
for (int i = 0; i < n; i++)
14+
if (a[i] != i)
15+
return i;
16+
return n;
17+
}
18+
};

single-number-iii.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Single Number III
2+
class Solution {
3+
public:
4+
vector<int> singleNumber(vector<int> &a) {
5+
int k = accumulate(a.begin(), a.end(), 0, bit_xor<int>());
6+
k &= -k;
7+
return {
8+
accumulate(a.begin(), a.end(), 0, [=](int s, int x) { return x & k ? s ^ x : s; }),
9+
accumulate(a.begin(), a.end(), 0, [=](int s, int x) { return x & k ? s : s ^ x; })
10+
};
11+
}
12+
};

ugly-number-ii.cc

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Ugly Number II
2+
class Solution {
3+
public:
4+
int nthUglyNumber(int n) {
5+
vector<int> a{1};
6+
int i = 0, j = 0, k = 0;
7+
while (a.size() < n) {
8+
int x = min(min(a[i]*2, a[j]*3), a[k]*5);
9+
a.push_back(x);
10+
if (a[i]*2 == x) i++;
11+
if (a[j]*3 == x) j++;
12+
if (a[k]*5 == x) k++;
13+
}
14+
return a[n-1];
15+
}
16+
};

ugly-number.cc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Ugly Number
2+
class Solution {
3+
public:
4+
bool isUgly(int num) {
5+
if (num) {
6+
while (num % 2 == 0) num /= 2;
7+
while (num % 3 == 0) num /= 3;
8+
while (num % 5 == 0) num /= 5;
9+
}
10+
return num == 1;
11+
}
12+
};

0 commit comments

Comments
 (0)