Skip to content

Commit acdf017

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 7ae9250 commit acdf017

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

039_combination_sum/combination_sum.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ static void dfs(int *nums, int size, int start, int target, int *stack,
1313
(*count)++;
1414
} else if (target > 0) {
1515
for (i = start; i < size; i++) {
16-
if (i > 0 && nums[i] == nums[i - 1]) {
17-
continue;
18-
}
1916
stack[len] = nums[i];
2017
dfs(nums, size, i, target - nums[i], stack, len + 1, results, column_sizes, count);
2118
}

040_combination_sum_ii/combination_sum.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ static void dfs(int *nums, int size, int start, int target, int *solution, int l
2020
} else if (target > 0) {
2121
for (i = start; i < size; i++) {
2222
if (!used[i]) {
23-
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) continue;
23+
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
24+
/* Forbid same elements in same level */
25+
/* Used marks allow same elements in different levels */
26+
continue;
27+
}
2428
used[i] = true;
2529
solution[len] = nums[i];
30+
/* i + 1 limits the selecting range in following levels */
2631
dfs(nums, size, i + 1, target - nums[i], solution, len + 1, used, results, count, column_sizes);
2732
used[i] = false;
2833
}

047_permutations_ii/permutations.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ static void dfs(int *nums, int size, bool *used, int *stack,
2020
} else {
2121
for (i = 0; i < size; i++) {
2222
if (!used[i]) {
23-
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
23+
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
24+
/* Forbid same elements on same level */
25+
/* Used marks allow same elements in different levels */
26+
continue;
27+
}
2428
used[i] = true;
2529
stack[len] = nums[i];
2630
dfs(nums, size, used, stack, len + 1, results, count, col_size);
@@ -33,7 +37,7 @@ static void dfs(int *nums, int size, bool *used, int *stack,
3337
/**
3438
* Return an array of arrays of size *returnSize.
3539
* The sizes of the arrays are returned as *returnColumnSizes array.
36-
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
40+
* Note: Both returned array and *returnColumnSizes array must be malloced, assume caller calls free().
3741
*/
3842
static int **permute(int* nums, int numsSize, int* returnSize, int **returnColumnSize)
3943
{

090_subsets_ii/subsets.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ static void dfs(int *nums, int size, int start, int *buf, int level,
1919
for (i = start; i < size; i++) {
2020
if (!used[i]) {
2121
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
22+
/* Forbid same elements on same level */
23+
/* Used marks allow same elements in different levels */
2224
continue;
2325
}
2426
used[i] = true;
2527
buf[level] = nums[i];
28+
/* i + 1 limits the selecting range in following levels */
2629
dfs(nums, size, i + 1, buf, level + 1, used, sets, count, sizes);
2730
used[i] = false;
2831
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy