Skip to content

Commit bb61f62

Browse files
committed
281/282
1 parent 031b255 commit bb61f62

22 files changed

+283
-109
lines changed

README.md

Lines changed: 2 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 280/281 problems.
5+
Solved 281/282 problems.
66

77
## Database
88

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

1313
| # | Title | Solution |
1414
|---| ----- | -------- |
15+
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/)|[bulls-and-cows.cc](bulls-and-cows.cc)|
1516
|297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)|[serialize-and-deserialize-binary-tree.cc](serialize-and-deserialize-binary-tree.cc)|
1617
|296|[Best Meeting Point](https://leetcode.com/problems/best-meeting-point/)|[best-meeting-point.cc](best-meeting-point.cc)|
1718
|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)|[find-median-from-data-stream.cc](find-median-from-data-stream.cc)|

bulls-and-cows.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Bulls and Cows
2+
class Solution {
3+
public:
4+
string getHint(string secret, string guess) {
5+
int c[10] = {}, a = 0, b = 0;
6+
for (int i = 0; i < secret.size(); i++) {
7+
if (secret[i] == guess[i])
8+
a++;
9+
else {
10+
if (c[secret[i]-'0']++ < 0) b++;
11+
if (c[guess[i]-'0']-- > 0) b++;
12+
}
13+
}
14+
return to_string(a)+"A"+to_string(b)+"B";
15+
}
16+
};

edit-distance.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,27 @@ class Solution {
1414
return f[a.size()&1][b.size()];
1515
}
1616
};
17+
18+
///
19+
20+
#define REP(i, n) for (int i = 0; i < (n); i++)
21+
22+
class Solution {
23+
public:
24+
int minDistance(string a, string b) {
25+
if (a.size() < b.size())
26+
swap(a, b);
27+
vector<int> d(b.size()+1);
28+
iota(d.begin(), d.end(), 0);
29+
REP(i, a.size()) {
30+
int ul = d[0];
31+
d[0] = i+1;
32+
REP(j, b.size()) {
33+
int t = d[j+1];
34+
d[j+1] = a[i] == b[j] ? ul : min(ul, min(d[j], d[j+1])) + 1;
35+
ul = t;
36+
}
37+
}
38+
return d.back();
39+
}
40+
};

jump-game-ii.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33

44
class Solution {
55
public:
6-
int jump(int A[], int n) {
6+
int jump(vector<int> &a) {
7+
int n = a.size();
78
vector<int> dp(n), q(n);
89
int *fore = &q[0], *rear = &q[0];
910
dp[*rear++ = 0] = 0;
1011
FOR(i, 1, n) {
11-
while (fore < rear && *fore+A[*fore] < i) fore++;
12+
while (fore < rear && *fore+a[*fore] < i) fore++;
1213
if (fore == rear) return -1;
1314
dp[i] = dp[*fore]+1;
14-
if (rear[-1]+A[rear[-1]] < i+A[i]) *rear++ = i;
15+
if (rear[-1]+a[rear[-1]] < i+a[i]) *rear++ = i;
1516
}
1617
return dp[n-1];
1718
};
1819
};
1920

20-
//
21+
///
2122

2223
class Solution {
2324
public:
24-
int jump(int A[], int n) {
25-
int f = 0, r = 0;
25+
int jump(vector<int> &a) {
26+
int n = a.size(), f = 0, r = 0;
2627
for (int i = 0; i < n && f < n-1; ) {
2728
int g = 0;
2829
for (; i <= f; i++)
29-
g = max(g, i+A[i]);
30+
g = max(g, i+a[i]);
3031
r++;
3132
f = g;
3233
}

jump-game.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
class Solution {
55
public:
6-
bool canJump(int A[], int n) {
6+
bool canJump(vector<int> &a) {
77
int r = 0;
8-
REP(i, n) {
8+
REP(i, a.size()) {
99
if (i > r) break;
10-
r = max(r, i+A[i]);
10+
r = max(r, i+a[i]);
1111
}
12-
return r >= n-1;
12+
return r >= a.size()-1;
1313
};
1414
};

maximum-product-subarray.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Maximum Product Subarray
2-
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
3-
42
class Solution {
53
public:
6-
int maxProduct(int A[], int n) {
7-
int ans = A[0], mi = A[0], ma = A[0];
8-
FOR(i, 1, n) {
9-
int x = mi*A[i], y = ma*A[i];
10-
mi = min(A[i], min(x, y));
11-
ma = max(A[i], max(x, y));
4+
int maxProduct(vector<int> &a) {
5+
int ans = a[0], mi = a[0], ma = a[0];
6+
for (int i = 1; i < a.size(); i++) {
7+
int x = mi*a[i], y = ma*a[i];
8+
mi = min(a[i], min(x, y));
9+
ma = max(a[i], max(x, y));
1210
ans = max(ans, ma);
1311
}
1412
return ans;

maximum-subarray.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
// Maximum Subarray
22
// Kadane's algorithm
3-
4-
#define REP(i, n) for (int i = 0; i < (n); i++)
5-
63
class Solution {
74
public:
8-
int maxSubArray(int A[], int n) {
9-
int r = A[0], s = 0;
10-
REP(i, n) {
11-
s = max(s+A[i], A[i]);
5+
int maxSubArray(vector<int> &a) {
6+
int r = a[0], s = 0;
7+
for (auto x: a) {
8+
s = max(s, 0) + x;
129
r = max(r, s);
1310
}
1411
return r;
1512
}
1613
};
17-

median-of-two-sorted-arrays.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Median of Two Sorted Arrays
22
class Solution {
33
public:
4-
double findMedianSortedArrays(int A[], int m, int B[], int n) {
5-
int i = 0, j = 0, k = m+n-1 >> 1;
4+
double findMedianSortedArrays(vector<int> &a, vector<int> &b) {
5+
int m = a.size(), n = b.size(), i = 0, j = 0, k = m+n-1 >> 1;
66
while (k > 0) {
77
int p = k-1 >> 1;
8-
if (j+p >= n || i+p < m && A[i+p] < B[j+p])
8+
if (j+p >= n || i+p < m && a[i+p] < b[j+p])
99
i += p+1;
1010
else
1111
j += p+1;
1212
k -= p+1;
1313
}
14-
int s = j >= n || i < m && A[i] < B[j] ? A[i++] : B[j++];
15-
return m+n & 1 ? s : (j >= n || i < m && A[i] < B[j] ? s+A[i] : s+B[j]) * 0.5;
14+
int s = j >= n || i < m && a[i] < b[j] ? a[i++] : b[j++];
15+
return m+n & 1 ? s : (j >= n || i < m && a[i] < b[j] ? s+a[i] : s+b[j]) * 0.5;
1616
}
1717
};

merge-sorted-array.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Merge Sorted Array
22
class Solution {
33
public:
4-
void merge(int A[], int m, int B[], int n) {
5-
int *C = new int[m+n];
4+
void merge(vector<int> &a, int m, vector<int> &b, int n) {
5+
vector<int> c(m+n);
66
for (int i = 0, j = 0; i < m || j < n; )
7-
if (j == n || i < m && A[i] < B[j])
8-
C[i+j] = A[i], i++;
7+
if (j == n || i < m && a[i] < b[j])
8+
c[i+j] = a[i], i++;
99
else
10-
C[i+j] = B[j], j++;
11-
copy(C, C+m+n, A);
12-
delete[] C;
10+
c[i+j] = b[j], j++;
11+
a.swap(c);
1312
}
1413
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Remove Duplicates from Sorted Array II
22
class Solution {
33
public:
4-
int removeDuplicates(int A[], int n) {
4+
int removeDuplicates(vector<int> &a) {
55
int j = 0;
6-
for (int i = 0; i < n; i++)
7-
if (j < 2 || A[j-1] != A[i] || A[j-2] != A[i])
8-
A[j++] = A[i];
6+
for (auto x: a)
7+
if (j < 2 || a[j-1] != x || a[j-2] != x)
8+
a[j++] = x;
99
return j;
1010
}
1111
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Remove Duplicates from Sorted Array
22
class Solution {
33
public:
4-
int removeDuplicates(int A[], int n) {
4+
int removeDuplicates(vector<int> &a) {
55
int j = 0;
6-
for (int i = 0; i < n; i++)
7-
if (!j || A[j-1] != A[i])
8-
A[j++] = A[i];
6+
for (auto x: a)
7+
if (!j || a[j-1] != x)
8+
a[j++] = x;
99
return j;
1010
}
1111
};

remove-element.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Remove Element
22
class Solution {
33
public:
4-
int removeElement(int A[], int n, int elem) {
4+
int removeElement(vector<int> &a, int val) {
55
int i = 0, j = 0;
6-
for (; i < n; i++)
7-
if (A[i] != elem)
8-
A[j++] = A[i];
6+
for (; i < a.size(); i++)
7+
if (a[i] != val)
8+
a[j++] = a[i];
99
return j;
1010
}
1111
};

rotate-array.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Rotate Array
22
class Solution {
33
public:
4-
void rotate(int a[], int n, int k) {
4+
void rotate(vector<int> &a, int k) {
5+
int n = a.size();
56
k = (n-k%n)%n;
6-
reverse(a, a+k);
7-
reverse(a+k, a+n);
8-
reverse(a, a+n);
7+
reverse(a.begin(), a.begin()+k);
8+
reverse(a.begin()+k, a.end());
9+
reverse(a.begin(), a.end());
910
}
1011
};
1112

@@ -24,7 +25,8 @@ int gcd(int a, int b)
2425

2526
class Solution {
2627
public:
27-
void rotate(int a[], int n, int m) {
28+
void rotate(vector<int> &a, int m) {
29+
int n = a.size();
2830
m %= n;
2931
int d = gcd(n, m);
3032
REP(i, d) {

search-for-a-range.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// Search for a Range
22
class Solution {
33
public:
4-
vector<int> searchRange(int A[], int n, int target) {
5-
int l = 0, m = n, h = n;
4+
vector<int> searchRange(vector<int> &a, int target) {
5+
int l = 0, m = a.size(), h = a.size();
66
while (l < m) {
77
int x = l+m >> 1;
8-
if (A[x] < target) l = x+1;
8+
if (a[x] < target) l = x+1;
99
else m = x;
1010
}
1111
vector<int> r;
12-
if (l == n || A[l] != target) {
12+
if (l == a.size() || a[l] != target) {
1313
r.push_back(-1);
1414
r.push_back(-1);
1515
} else {
1616
while (m < h) {
1717
int x = m+h >> 1;
18-
if (A[x] <= target) m = x+1;
18+
if (a[x] <= target) m = x+1;
1919
else h = x;
2020
}
2121
r.push_back(l);

search-in-rotated-sorted-array-ii.cc

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
// Search in Rotated Sorted Array II
2+
23
class Solution {
34
public:
4-
bool search(int A[], int n, int target) {
5-
int l = 0, h = n;
5+
bool search(vector<int> &a, int target) {
6+
int l = 0, h = a.size();
7+
while (l < h) {
8+
while (l+1 < h && a[l] == a[l+1])
9+
l++;
10+
int m = l+h >> 1;
11+
if (a[m] < target) {
12+
if (target < a[l] || a[l] < a[m])
13+
l = m+1;
14+
else
15+
h = m;
16+
} else if (a[m] > target) {
17+
if (a[l] <= target || a[l] >= a[m])
18+
h = m;
19+
else
20+
l = m+1;
21+
} else
22+
return true;
23+
}
24+
return false;
25+
}
26+
};
27+
28+
///
29+
30+
class Solution {
31+
public:
32+
bool search(vector<int> &a, int target) {
33+
int l = 0, h = a.size();
634
while (l < h) {
735
int m = l+h >> 1;
8-
if (A[m] == target) return true;
9-
if (A[l] < A[m]) {
10-
if (A[l] <= target && target < A[m])
36+
if (a[m] == target) return true;
37+
if (a[l] < a[m]) {
38+
if (a[l] <= target && target < a[m])
1139
h = m;
1240
else
1341
l = m+1;
14-
} else if (A[l] > A[m]) {
15-
if (A[m] < target && target <= A[h-1])
42+
} else if (a[l] > a[m]) {
43+
if (a[m] < target && target <= a[h-1])
1644
l = m+1;
1745
else
1846
h = m;
1947
} else
2048
l++;
2149
}
22-
return l < n && A[l] == target;
50+
return false;
2351
}
2452
};

0 commit comments

Comments
 (0)