Movatterモバイル変換


[0]ホーム

URL:


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

Commit15d6c5f

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent4fab7e0 commit15d6c5f

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 beduplicate for the purpose of the problem*/
23+
/* The elements in solution can betaken 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
inti,right=0;
1313
intsteps=0;
1414
intfartest=0;
15-
/* 1. Exhaust all the right boundries in the location range of [i...right]
16-
* 2. Whenthe search ends up with i==right, update theright boundry as
17-
*thefartest 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. Wheni reaches the farthest boundary, update thefarthest boundry
17+
*andthestep 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. Whenthe search ends up with i==right, update theright boundry as
13-
//thefartest 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. Wheni reaches the farthest boundary, update thefarthest boundry
13+
//andthestep 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
unsignedintlo=1;
6161
unsignedinthi= (unsignedint)x;
6262
unsignedintmid=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
unsignedint lo =1, hi = x;
1313
unsignedint 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-
intneed_to_meet =t.length();
15-
int start, min_len = INT_MAX;
14+
inthit_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
returnmask;

‎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
staticuint32_treverseBits(uint32_tn)
66
{
7-
inti;
8-
uint32_tres=0;
9-
for (i=0;i<32;i++) {
10-
res <<=1;
11-
res |=n&0x1;
12-
n >>=1;
13-
}
14-
returnres;
7+
constuint32_tMASK1=0x55555555;
8+
constuint32_tMASK2=0x33333333;
9+
constuint32_tMASK4=0x0f0f0f0f;
10+
constuint32_tMASK8=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+
returnn <<16 |n >>16;
1518
}
1619

1720
intmain(intargc,char**argv)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp