Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit db352a9

Browse files
Update comments
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 1c913a4 commit db352a9

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

0046_permutations/permutations.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ static void dfs(int *nums, int size, bool *used, int *stack,
4141
memcpy(results[*count], stack, size * sizeof(int));
4242
col_size[*count] = size;
4343
(*count)++;
44-
} else {
45-
/* Reverse order is allowed in different levels, always starts from [0] */
46-
for (i = 0; i < size; i++) {
47-
if (!used[i]) {
48-
/* Used marks only allows remaining elements in DFS levels */
49-
used[i] = true;
50-
stack[len] = nums[i];
51-
dfs(nums, size, used, stack, len + 1, results, count, col_size);
52-
used[i] = false;
53-
}
44+
return;
45+
}
46+
47+
/* Reverse order is allowed in different levels, always starts from [0] */
48+
for (i = 0; i < size; i++) {
49+
if (!used[i]) {
50+
/* Used marks all the allowable remaining elements in the next DFS
51+
* levels */
52+
stack[len] = nums[i];
53+
used[i] = true;
54+
dfs(nums, size, used, stack, len + 1, results, count, col_size);
55+
used[i] = false;
5456
}
5557
}
5658
}

0046_permutations/permutations.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ class Solution {
1616
void dfs(vector<int>& nums, vector<bool>& used, vector<vector<int>>& res) {
1717
if (stack.size() == nums.size()) {
1818
res.push_back(stack);
19-
} else {
20-
// Reverse order is allowed in different levels, always starts from [0]
21-
for (int i = 0; i < nums.size(); i++) {
22-
if (!used[i]) {
23-
// Used marks only allows remaining elements in DFS levels
24-
used[i] = true;
25-
stack.push_back(nums[i]);
26-
dfs(nums, used, res);
27-
stack.pop_back();
28-
used[i] = false;
29-
}
19+
return;
20+
}
21+
22+
// Reverse order is allowed in different levels, always starts from [0]
23+
for (int i = 0; i < nums.size(); i++) {
24+
if (!used[i]) {
25+
// Used marks all the allowable remaining elements in the next
26+
// DFS levels
27+
used[i] = true;
28+
stack.push_back(nums[i]);
29+
dfs(nums, used, res);
30+
stack.pop_back();
31+
used[i] = false;
3032
}
3133
}
3234
}

0047_permutations_ii/permutations.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,29 @@ static void dfs(int *nums, int size, bool *used, int *stack,
1212
int len, int **results, int *count, int *col_size)
1313
{
1414
int i;
15+
1516
if (len == size) {
1617
results[*count] = malloc(len * sizeof(int));
1718
memcpy(results[*count], stack, len * sizeof(int));
1819
col_size[*count] = size;
1920
(*count)++;
20-
} else {
21-
/* Reverse order is allowed in different levels, always starts from [0] */
22-
for (i = 0; i < size; i++) {
23-
/* Used marks only allows remaining elements in DFS levels */
24-
if (!used[i]) {
25-
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
26-
/* In case duplicate permutation with same elemements in the same postion */
27-
/* used[i - 1] == true means different level position */
28-
continue;
29-
}
30-
used[i] = true;
31-
stack[len] = nums[i];
32-
dfs(nums, size, used, stack, len + 1, results, count, col_size);
33-
used[i] = false;
21+
return;
22+
}
23+
24+
/* Reverse order is allowed in different levels, always starts from [0] */
25+
for (i = 0; i < size; i++) {
26+
/* used marks remaining allowable elements in the next DFS level */
27+
if (!used[i]) {
28+
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
29+
/* !used[i - 1] means the upper DFS level does not contain
30+
* nums[i - 1] such that we need to exclude the duplicate
31+
* enumeration that has been recorded with used[i - 1]==true. */
32+
continue;
3433
}
34+
stack[len] = nums[i];
35+
used[i] = true;
36+
dfs(nums, size, used, stack, len + 1, results, count, col_size);
37+
used[i] = false;
3538
}
3639
}
3740
}

0047_permutations_ii/permutations.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ class Solution {
1919
res.push_back(stack);
2020
} else {
2121
for (int i = 0; i < nums.size(); i++) {
22-
// Used marks only allows remaining elements in DFS levels
22+
// used marks remaining allowable elements in the next DFS level
2323
if (!used[i]) {
2424
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
25-
// In case duplicate permutation with same elemements in the same postion
26-
// used[i - 1] == true means different level position
25+
// !used[i - 1] means the upper DFS level does not contain
26+
// nums[i - 1] such that we need to exclude the duplicate
27+
// enumeration that has been recorded with used[i-1]==true.
2728
continue;
2829
}
2930
stack.push_back(nums[i]);

0 commit comments

Comments
 (0)