Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 15d6c5f

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 4fab7e0 commit 15d6c5f

File tree

8 files changed

+40
-25
lines changed

8 files changed

+40
-25
lines changed

‎0039_combination_sum/combination_sum.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Solution {
2020
} else {
2121
for (int i = start; i < candidates.size(); i++) {
2222
stack.push_back(candidates[i]);
23-
/* The elements in solution can be duplicate for the purpose of the problem */
23+
/* The elements in solution can be taken as many times as you can for the purpose of the problem */
2424
dfs(candidates, i, target - candidates[i], res);
2525
stack.pop_back();
2626
}

‎0045_jump_game_ii/jump_game.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ static int jump(int* nums, int numsSize)
1212
int i, right = 0;
1313
int steps = 0;
1414
int fartest = 0;
15-
/* 1. Exhaust all the right boundries in the location range of [i...right]
16-
* 2. When the search ends up with i==right, update the right boundry as
17-
* the fartest position.
18-
* 3. When the search ends up with i==right, it records as one jump step */
15+
/* 1. Exhaust all the right boundries in the location range of [i...farthest]
16+
* 2. When i reaches the farthest boundary, update the farthest boundry
17+
* and the step number.
18+
* 3. Apply condition i < size - 1 and iterator i++ to avoid overflow. */
1919
for (i = 0; i < numsSize; i++) {
2020
fartest = max(i + nums[i], fartest);
2121
if (i == right) {

‎0045_jump_game_ii/jump_game.cc‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class Solution {
88
int steps = 0;
99
int right = 0;
1010
int farthest = 0;
11-
// 1. Exhaust all the right boundries in the location range of [i...right]
12-
// 2. When the search ends up with i==right, update the right boundry as
13-
// the fartest position.
14-
// 3. When the search ends up with i==right, it records as one jump step */
11+
// 1. Exhaust all the right boundries in the location range of [i...farthest]
12+
// 2. When i reaches the farthest boundary, update the farthest boundry
13+
// and the step number.
14+
// 3. Apply condition i < size - 1 and iterator i++ to avoid overflow.
1515
for (int i = 0; i < nums.size() - 1; i++) {
16-
fartest = max(i + nums[i], fartest);
17-
for (i == right) {
18-
right = fartest;
16+
right = max(i + nums[i], right);
17+
if (i == farthest) {
18+
farthest = right;
1919
steps++;
2020
}
2121
}

‎0069_sqrt/sqrt.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ int mySqrt(int x)
6060
unsigned int lo = 1;
6161
unsigned int hi = (unsigned int) x;
6262
unsigned int mid = lo + (hi - lo) / 2;
63+
// Firstly test mid > x / mid to decide whether hi = mid;
64+
// else then test mid + 1 > x / (mid + 1) to decide whether the mid is located;
65+
// Otherwise assign low = mid.
6366
for (; ;) {
6467
if (mid > x/mid) {
6568
hi = mid;

‎0069_sqrt/sqrt.cc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class Solution {
1111

1212
unsigned int lo = 1, hi = x;
1313
unsigned int mid = (lo + hi) / 2;
14+
// Firstly test mid > x / mid to decide whether hi = mid;
15+
// else then test mid + 1 > x / (mid + 1) to decide whether the mid is located;
16+
// Otherwise assign low = mid.
1417
for (; ;) {
1518
if (mid > x / mid) {
1619
hi = mid;

‎0076_minimum_window_substring/window_substring.cc‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ class Solution {
1111
}
1212

1313
int l = 0, r = 0;
14-
int need_to_meet = t.length();
15-
int start, min_len = INT_MAX;
14+
int hit_num = 0;
15+
int start = 0, min_len = INT_MAX;
1616
while (r < s.length()) {
17+
// counting each letter in the string. The zero and positive
18+
// countings indicate ones in pattern. And the negative ones
19+
// indicate those out of the pattern.
1720
if (--count[s[r++]] >= 0) {
18-
need_to_meet--;
21+
hit_num++;
1922
}
2023

21-
while (need_to_meet == 0) {
24+
while (hit_num == t.length()) {
2225
if (r - l < min_len) {
2326
start = l;
2427
min_len = r - l;
2528
}
29+
// The countings of the letter larger than zero shall be
30+
// the ones in the pattern.
2631
if (++count[s[l++]] > 0) {
27-
need_to_meet++;
32+
hit_num--;
2833
}
2934
}
3035
}

‎0137_single_number_ii/single_number.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static int singleNumber(int *nums, int numsSize)
3939
count[i]++;
4040
}
4141
}
42+
/* The specified bit counting should be multiple of 3 without the outlier */
4243
mask |= (count[i] % 3) << i;
4344
}
4445
return mask;

‎0190_reverse_bits/reverse_bits.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
static uint32_t reverseBits(uint32_t n)
66
{
7-
int i;
8-
uint32_t res = 0;
9-
for (i = 0; i < 32; i++) {
10-
res <<= 1;
11-
res |= n & 0x1;
12-
n >>= 1;
13-
}
14-
return res;
7+
const uint32_t MASK1 = 0x55555555;
8+
const uint32_t MASK2 = 0x33333333;
9+
const uint32_t MASK4 = 0x0f0f0f0f;
10+
const uint32_t MASK8 = 0x00ff00ff;
11+
12+
// Extract and swap the even and odd bit groups.
13+
n = (n & MASK1) << 1 | ((n >> 1) & MASK1);
14+
n = (n & MASK2) << 2 | ((n >> 2) & MASK2);
15+
n = (n & MASK4) << 4 | ((n >> 4) & MASK4);
16+
n = (n & MASK8) << 8 | ((n >> 8) & MASK8);
17+
return n << 16 | n >> 16;
1518
}
1619

1720
int main(int argc, char **argv)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /