We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4fab7e0 commit 15d6c5fCopy full SHA for 15d6c5f
0039_combination_sum/combination_sum.cc
@@ -20,7 +20,7 @@ class Solution {
20
} else {
21
for (int i = start; i < candidates.size(); i++) {
22
stack.push_back(candidates[i]);
23
- /* The elements in solution can be duplicate for the purpose of the problem */
+ /* The elements in solution can be taken as many times as you can for the purpose of the problem */
24
dfs(candidates, i, target - candidates[i], res);
25
stack.pop_back();
26
}
0045_jump_game_ii/jump_game.c
@@ -12,10 +12,10 @@ static int jump(int* nums, int numsSize)
12
int i, right = 0;
13
int steps = 0;
14
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 */
+ /* 1. Exhaust all the right boundries in the location range of [i...farthest]
+ * 2. When i reaches the farthest boundary, update the farthest boundry
+ * and the step number.
+ * 3. Apply condition i < size - 1 and iterator i++ to avoid overflow. */
19
for (i = 0; i < numsSize; i++) {
fartest = max(i + nums[i], fartest);
if (i == right) {
0045_jump_game_ii/jump_game.cc
@@ -8,14 +8,14 @@ class Solution {
8
9
int right = 0;
10
int farthest = 0;
11
- // 1. Exhaust all the right boundries in the location range of [i...right]
- // 2. When the search ends up with i==right, update the right boundry as
- // the fartest position.
- // 3. When the search ends up with i==right, it records as one jump step */
+ // 1. Exhaust all the right boundries in the location range of [i...farthest]
+ // 2. When i reaches the farthest boundary, update the farthest boundry
+ // and the step number.
+ // 3. Apply condition i < size - 1 and iterator i++ to avoid overflow.
for (int i = 0; i < nums.size() - 1; i++) {
- fartest = max(i + nums[i], fartest);
- for (i == right) {
- right = fartest;
+ right = max(i + nums[i], right);
+ if (i == farthest) {
+ farthest = right;
steps++;
0069_sqrt/sqrt.c
@@ -60,6 +60,9 @@ int mySqrt(int x)
60
unsigned int lo = 1;
61
unsigned int hi = (unsigned int) x;
62
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.
66
for (; ;) {
67
if (mid > x/mid) {
68
hi = mid;
0069_sqrt/sqrt.cc
@@ -11,6 +11,9 @@ class Solution {
unsigned int lo = 1, hi = x;
unsigned int mid = (lo + hi) / 2;
if (mid > x / mid) {
0076_minimum_window_substring/window_substring.cc
@@ -11,20 +11,25 @@ class Solution {
int l = 0, r = 0;
- int need_to_meet = t.length();
- int start, min_len = INT_MAX;
+ int hit_num = 0;
+ int start = 0, min_len = INT_MAX;
while (r < s.length()) {
+ // counting each letter in the string. The zero and positive
+ // countings indicate ones in pattern. And the negative ones
+ // indicate those out of the pattern.
if (--count[s[r++]] >= 0) {
- need_to_meet--;
+ hit_num++;
- while (need_to_meet == 0) {
+ while (hit_num == t.length()) {
if (r - l < min_len) {
start = l;
27
min_len = r - l;
28
29
+ // The countings of the letter larger than zero shall be
30
+ // the ones in the pattern.
31
if (++count[s[l++]] > 0) {
- need_to_meet++;
32
+ hit_num--;
33
34
35
0137_single_number_ii/single_number.c
@@ -39,6 +39,7 @@ static int singleNumber(int *nums, int numsSize)
39
count[i]++;
40
41
42
+ /* The specified bit counting should be multiple of 3 without the outlier */
43
mask |= (count[i] % 3) << i;
44
45
return mask;
0190_reverse_bits/reverse_bits.c
@@ -4,14 +4,17 @@
4
5
static uint32_t reverseBits(uint32_t n)
6
{
7
- int i;
- uint32_t res = 0;
- for (i = 0; i < 32; i++) {
- res <<= 1;
- res |= n & 0x1;
- n >>= 1;
- }
- return res;
+ const uint32_t MASK1 = 0x55555555;
+ const uint32_t MASK2 = 0x33333333;
+ const uint32_t MASK4 = 0x0f0f0f0f;
+ const uint32_t MASK8 = 0x00ff00ff;
+
+ // Extract and swap the even and odd bit groups.
+ n = (n & MASK1) << 1 | ((n >> 1) & MASK1);
+ n = (n & MASK2) << 2 | ((n >> 2) & MASK2);
+ n = (n & MASK4) << 4 | ((n >> 4) & MASK4);
+ n = (n & MASK8) << 8 | ((n >> 8) & MASK8);
+ return n << 16 | n >> 16;
int main(int argc, char **argv)
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments