Files
hello-algo/en/codes/c/chapter_backtracking/permutations_ii.c
Yudong Jin 2778a6f9c7 Translate all code to English (#1836)
* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
2025-12-31 07:44:52 +08:00

82 lines
2.4 KiB
C

/**
* File: permutations_ii.c
* Created Time: 2023-10-17
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.h"
// Assume at most 1000 permutations, max element value 1000
#define MAX_SIZE 1000
/* Backtracking algorithm: Permutations II */
void backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {
// When the state length equals the number of elements, record the solution
if (stateSize == choicesSize) {
res[*resSize] = (int *)malloc(choicesSize * sizeof(int));
for (int i = 0; i < choicesSize; i++) {
res[*resSize][i] = state[i];
}
(*resSize)++;
return;
}
// Traverse all choices
bool duplicated[MAX_SIZE] = {false};
for (int i = 0; i < choicesSize; i++) {
int choice = choices[i];
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
if (!selected[i] && !duplicated[choice]) {
// Attempt: make choice, update state
duplicated[choice] = true; // Record the selected element value
selected[i] = true;
state[stateSize] = choice;
// Proceed to the next round of selection
backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);
// Backtrack: undo choice, restore to previous state
selected[i] = false;
}
}
}
/* Permutations II */
int **permutationsII(int *nums, int numsSize, int *returnSize) {
int *state = (int *)malloc(numsSize * sizeof(int));
bool *selected = (bool *)malloc(numsSize * sizeof(bool));
for (int i = 0; i < numsSize; i++) {
selected[i] = false;
}
int **res = (int **)malloc(MAX_SIZE * sizeof(int *));
*returnSize = 0;
backtrack(state, 0, nums, numsSize, selected, res, returnSize);
free(state);
free(selected);
return res;
}
/* Driver Code */
int main() {
int nums[] = {1, 1, 2};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int returnSize;
int **res = permutationsII(nums, numsSize, &returnSize);
printf("Input array nums = ");
printArray(nums, numsSize);
printf("\nAll permutations res = \n");
for (int i = 0; i < returnSize; i++) {
printArray(res[i], numsSize);
}
// Free memory
for (int i = 0; i < returnSize; i++) {
free(res[i]);
}
free(res);
return 0;
}