mirror of
https://github.com/krahets/hello-algo.git
synced 2026-01-12 00:04:24 +08:00
* 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
80 lines
2.2 KiB
C
80 lines
2.2 KiB
C
/**
|
|
* File: permutations_i.c
|
|
* Created Time: 2023-06-04
|
|
* Author: Gonglja (glj0@outlook.com), krahets (krahets@163.com)
|
|
*/
|
|
|
|
#include "../utils/common.h"
|
|
|
|
// Assume at most 1000 permutations
|
|
#define MAX_SIZE 1000
|
|
|
|
/* Backtracking algorithm: Permutations I */
|
|
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
|
|
for (int i = 0; i < choicesSize; i++) {
|
|
int choice = choices[i];
|
|
// Pruning: do not allow repeated selection of elements
|
|
if (!selected[i]) {
|
|
// Attempt: make choice, update state
|
|
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 I */
|
|
int **permutationsI(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, 2, 3};
|
|
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
|
int returnSize;
|
|
|
|
int **res = permutationsI(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;
|
|
}
|