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

[pull] master from begeekmyfriend:master #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 1 commit into AlgorithmAndLeetCode:master from begeekmyfriend:master
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions 0046_permutations/permutations.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ static void dfs(int *nums, int size, bool *used, int *stack,
memcpy(results[*count], stack, size * sizeof(int));
col_size[*count] = size;
(*count)++;
} else {
/* Reverse order is allowed in different levels, always starts from [0] */
for (i = 0; i < size; i++) {
if (!used[i]) {
/* Used marks only allows remaining elements in DFS levels */
used[i] = true;
stack[len] = nums[i];
dfs(nums, size, used, stack, len + 1, results, count, col_size);
used[i] = false;
}
return;
}

/* Reverse order is allowed in different levels, always starts from [0] */
for (i = 0; i < size; i++) {
if (!used[i]) {
/* Used marks all the allowable remaining elements in the next DFS
* levels */
stack[len] = nums[i];
used[i] = true;
dfs(nums, size, used, stack, len + 1, results, count, col_size);
used[i] = false;
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions 0046_permutations/permutations.cc
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ class Solution {
void dfs(vector<int>& nums, vector<bool>& used, vector<vector<int>>& res) {
if (stack.size() == nums.size()) {
res.push_back(stack);
} else {
// Reverse order is allowed in different levels, always starts from [0]
for (int i = 0; i < nums.size(); i++) {
if (!used[i]) {
// Used marks only allows remaining elements in DFS levels
used[i] = true;
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = false;
}
return;
}

// Reverse order is allowed in different levels, always starts from [0]
for (int i = 0; i < nums.size(); i++) {
if (!used[i]) {
// Used marks all the allowable remaining elements in the next
// DFS levels
used[i] = true;
stack.push_back(nums[i]);
dfs(nums, used, res);
stack.pop_back();
used[i] = false;
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions 0047_permutations_ii/permutations.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@ static void dfs(int *nums, int size, bool *used, int *stack,
int len, int **results, int *count, int *col_size)
{
int i;

if (len == size) {
results[*count] = malloc(len * sizeof(int));
memcpy(results[*count], stack, len * sizeof(int));
col_size[*count] = size;
(*count)++;
} else {
/* Reverse order is allowed in different levels, always starts from [0] */
for (i = 0; i < size; i++) {
/* Used marks only allows remaining elements in DFS levels */
if (!used[i]) {
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
/* In case duplicate permutation with same elemements in the same postion */
/* used[i - 1] == true means different level position */
continue;
}
used[i] = true;
stack[len] = nums[i];
dfs(nums, size, used, stack, len + 1, results, count, col_size);
used[i] = false;
return;
}

/* Reverse order is allowed in different levels, always starts from [0] */
for (i = 0; i < size; i++) {
/* used marks remaining allowable elements in the next DFS level */
if (!used[i]) {
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
/* !used[i - 1] means the upper DFS level does not contain
* nums[i - 1] such that we need to exclude the duplicate
* enumeration that has been recorded with used[i - 1]==true. */
continue;
}
stack[len] = nums[i];
used[i] = true;
dfs(nums, size, used, stack, len + 1, results, count, col_size);
used[i] = false;
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions 0047_permutations_ii/permutations.cc
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class Solution {
res.push_back(stack);
} else {
for (int i = 0; i < nums.size(); i++) {
// Used marks only allows remaining elements in DFS levels
// used marks remaining allowable elements in the next DFS level
if (!used[i]) {
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
// In case duplicate permutation with same elemements in the same postion
// used[i - 1] == true means different level position
// !used[i - 1] means the upper DFS level does not contain
// nums[i - 1] such that we need to exclude the duplicate
// enumeration that has been recorded with used[i-1]==true.
continue;
}
stack.push_back(nums[i]);
Expand Down

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