mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 14:10:57 +00:00
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
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
add_executable(permutations_i permutations_i.c)
|
||||
add_executable(permutations_ii permutations_ii.c)
|
||||
add_executable(preorder_traversal_i_compact preorder_traversal_i_compact.c)
|
||||
add_executable(preorder_traversal_ii_compact preorder_traversal_ii_compact.c)
|
||||
add_executable(preorder_traversal_iii_compact preorder_traversal_iii_compact.c)
|
||||
add_executable(preorder_traversal_iii_template preorder_traversal_iii_template.c)
|
||||
add_executable(subset_sum_i_naive subset_sum_i_naive.c)
|
||||
add_executable(subset_sum_i subset_sum_i.c)
|
||||
add_executable(subset_sum_ii subset_sum_ii.c)
|
||||
add_executable(n_queens n_queens.c)
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* File : n_queens.c
|
||||
* Created Time: 2023-09-25
|
||||
* Author : lucas (superrat6@gmail.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
/* Backtracking algorithm: N queens */
|
||||
void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],
|
||||
bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {
|
||||
// When all rows are placed, record the solution
|
||||
if (row == n) {
|
||||
res[*resSize] = (char **)malloc(sizeof(char *) * n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));
|
||||
strcpy(res[*resSize][i], state[i]);
|
||||
}
|
||||
(*resSize)++;
|
||||
return;
|
||||
}
|
||||
// Traverse all columns
|
||||
for (int col = 0; col < n; col++) {
|
||||
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// Attempt: place the queen in this cell
|
||||
state[row][col] = 'Q';
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = true;
|
||||
// Place the next row
|
||||
backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);
|
||||
// Backtrack: restore this cell to an empty cell
|
||||
state[row][col] = '#';
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve N queens */
|
||||
char ***nQueens(int n, int *returnSize) {
|
||||
char state[MAX_SIZE][MAX_SIZE];
|
||||
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
state[i][j] = '#';
|
||||
}
|
||||
state[i][n] = '\0';
|
||||
}
|
||||
bool cols[MAX_SIZE] = {false}; // Record whether there is a queen in the column
|
||||
bool diags1[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the main diagonal
|
||||
bool diags2[2 * MAX_SIZE - 1] = {false}; // Record whether there is a queen on the anti-diagonal
|
||||
|
||||
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
|
||||
*returnSize = 0;
|
||||
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 4;
|
||||
int returnSize;
|
||||
char ***res = nQueens(n, &returnSize);
|
||||
|
||||
printf("Input board size is %d\n", n);
|
||||
printf("Total queen placement solutions: %d\n", returnSize);
|
||||
for (int i = 0; i < returnSize; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
printf("[");
|
||||
for (int k = 0; res[i][j][k] != '\0'; ++k) {
|
||||
printf("%c", res[i][j][k]);
|
||||
if (res[i][j][k + 1] != '\0') {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
printf("---------------------\n");
|
||||
}
|
||||
|
||||
// Free memory
|
||||
for (int i = 0; i < returnSize; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
free(res[i][j]);
|
||||
}
|
||||
free(res[i]);
|
||||
}
|
||||
free(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_compact.c
|
||||
* Created Time: 2023-05-10
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// Assume result length not exceeding 100
|
||||
#define MAX_SIZE 100
|
||||
|
||||
TreeNode *res[MAX_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* Preorder traversal: Example 1 */
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
if (root->val == 7) {
|
||||
// Record solution
|
||||
res[resSize++] = root;
|
||||
}
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\nInitialize binary tree\n");
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
preOrder(root);
|
||||
|
||||
printf("\nOutput all nodes with value 7\n");
|
||||
int *vals = malloc(resSize * sizeof(int));
|
||||
for (int i = 0; i < resSize; i++) {
|
||||
vals[i] = res[i]->val;
|
||||
}
|
||||
printArray(vals, resSize);
|
||||
|
||||
// Free memory
|
||||
freeMemoryTree(root);
|
||||
free(vals);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* File: preorder_traversal_ii_compact.c
|
||||
* Created Time: 2023-05-28
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// Assume path and result length not exceeding 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* Preorder traversal: Example 2 */
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path[pathSize++] = root;
|
||||
if (root->val == 7) {
|
||||
// Record solution
|
||||
for (int i = 0; i < pathSize; ++i) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
resSize++;
|
||||
}
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
// Backtrack
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\nInitialize binary tree\n");
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
preOrder(root);
|
||||
|
||||
printf("\nOutput all paths from root to node 7\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int *vals = malloc(MAX_SIZE * sizeof(int));
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printArray(vals, size);
|
||||
free(vals);
|
||||
}
|
||||
|
||||
// Free memory
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_compact.c
|
||||
* Created Time: 2023-06-04
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// Assume path and result length not exceeding 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* Preorder traversal: Example 3 */
|
||||
void preOrder(TreeNode *root) {
|
||||
// Pruning
|
||||
if (root == NULL || root->val == 3) {
|
||||
return;
|
||||
}
|
||||
// Attempt
|
||||
path[pathSize++] = root;
|
||||
if (root->val == 7) {
|
||||
// Record solution
|
||||
for (int i = 0; i < pathSize; i++) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
resSize++;
|
||||
}
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
// Backtrack
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\nInitialize binary tree\n");
|
||||
printTree(root);
|
||||
|
||||
// Preorder traversal
|
||||
preOrder(root);
|
||||
|
||||
printf("\nOutput all paths from root to node 7, excluding nodes with value 3\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int *vals = malloc(MAX_SIZE * sizeof(int));
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printArray(vals, size);
|
||||
free(vals);
|
||||
}
|
||||
|
||||
// Free memory
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* File: preorder_traversal_iii_template.c
|
||||
* Created Time: 2023-06-04
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// Assume path and result length not exceeding 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* Check if the current state is a solution */
|
||||
bool isSolution(void) {
|
||||
return pathSize > 0 && path[pathSize - 1]->val == 7;
|
||||
}
|
||||
|
||||
/* Record solution */
|
||||
void recordSolution(void) {
|
||||
for (int i = 0; i < pathSize; i++) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
resSize++;
|
||||
}
|
||||
|
||||
/* Check if the choice is valid under the current state */
|
||||
bool isValid(TreeNode *choice) {
|
||||
return choice != NULL && choice->val != 3;
|
||||
}
|
||||
|
||||
/* Update state */
|
||||
void makeChoice(TreeNode *choice) {
|
||||
path[pathSize++] = choice;
|
||||
}
|
||||
|
||||
/* Restore state */
|
||||
void undoChoice(void) {
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* Backtracking algorithm: Example 3 */
|
||||
void backtrack(TreeNode *choices[2]) {
|
||||
// Check if it is a solution
|
||||
if (isSolution()) {
|
||||
// Record solution
|
||||
recordSolution();
|
||||
}
|
||||
// Traverse all choices
|
||||
for (int i = 0; i < 2; i++) {
|
||||
TreeNode *choice = choices[i];
|
||||
// Pruning: check if the choice is valid
|
||||
if (isValid(choice)) {
|
||||
// Attempt: make choice, update state
|
||||
makeChoice(choice);
|
||||
// Proceed to the next round of selection
|
||||
TreeNode *nextChoices[2] = {choice->left, choice->right};
|
||||
backtrack(nextChoices);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
undoChoice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\nInitialize binary tree\n");
|
||||
printTree(root);
|
||||
|
||||
// Backtracking algorithm
|
||||
TreeNode *choices[2] = {root, NULL};
|
||||
backtrack(choices);
|
||||
|
||||
printf("\nOutput all paths from root to node 7, excluding nodes with value 3\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int *vals = malloc(MAX_SIZE * sizeof(int));
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printArray(vals, size);
|
||||
free(vals);
|
||||
}
|
||||
|
||||
// Free memory
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* File: subset_sum_i.c
|
||||
* Created Time: 2023-07-29
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// State (subset)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// Result list (subset list)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
void backtrack(int target, int *choices, int choicesSize, int start) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
for (int i = 0; i < stateSize; ++i) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
for (int i = start; i < choicesSize; i++) {
|
||||
// Pruning 1: if the subset sum exceeds target, end the loop directly
|
||||
// This is because the array is sorted, and later elements are larger, so the subset sum will definitely exceed target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// Attempt: make choice, update target, start
|
||||
state[stateSize] = choices[i];
|
||||
stateSize++;
|
||||
// Proceed to the next round of selection
|
||||
backtrack(target - choices[i], choices, choicesSize, i);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Comparison function */
|
||||
int cmp(const void *a, const void *b) {
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
|
||||
/* Solve subset sum I */
|
||||
void subsetSumI(int *nums, int numsSize, int target) {
|
||||
qsort(nums, numsSize, sizeof(int), cmp); // Sort nums
|
||||
int start = 0; // Start point for traversal
|
||||
backtrack(target, nums, numsSize, start);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {3, 4, 5};
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
subsetSumI(nums, numsSize, target);
|
||||
|
||||
printf("Input array nums = ");
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("All subsets with sum equal to %d res = \n", target);
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* File: subset_sum_i_naive.c
|
||||
* Created Time: 2023-07-28
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// State (subset)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// Result list (subset list)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* Backtracking algorithm: Subset sum I */
|
||||
void backtrack(int target, int total, int *choices, int choicesSize) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (total == target) {
|
||||
for (int i = 0; i < stateSize; i++) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
for (int i = 0; i < choicesSize; i++) {
|
||||
// Pruning: if the subset sum exceeds target, skip this choice
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// Attempt: make choice, update element sum total
|
||||
state[stateSize++] = choices[i];
|
||||
// Proceed to the next round of selection
|
||||
backtrack(target, total + choices[i], choices, choicesSize);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve subset sum I (including duplicate subsets) */
|
||||
void subsetSumINaive(int *nums, int numsSize, int target) {
|
||||
resSize = 0; // Initialize solution count to 0
|
||||
backtrack(target, 0, nums, numsSize);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {3, 4, 5};
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
subsetSumINaive(nums, numsSize, target);
|
||||
|
||||
printf("Input array nums = ");
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("All subsets with sum equal to %d res = \n", target);
|
||||
for (int i = 0; i < resSize; i++) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* File: subset_sum_ii.c
|
||||
* Created Time: 2023-07-29
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// State (subset)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// Result list (subset list)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* Backtracking algorithm: Subset sum II */
|
||||
void backtrack(int target, int *choices, int choicesSize, int start) {
|
||||
// When the subset sum equals target, record the solution
|
||||
if (target == 0) {
|
||||
for (int i = 0; i < stateSize; i++) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// Traverse all choices
|
||||
// Pruning 2: start traversing from start to avoid generating duplicate subsets
|
||||
// Pruning 3: start traversing from start to avoid repeatedly selecting the same element
|
||||
for (int i = start; i < choicesSize; i++) {
|
||||
// Pruning 1: Skip if subset sum exceeds target
|
||||
if (target - choices[i] < 0) {
|
||||
continue;
|
||||
}
|
||||
// Pruning 4: if this element equals the left element, it means this search branch is duplicate, skip it directly
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// Attempt: make choice, update target, start
|
||||
state[stateSize] = choices[i];
|
||||
stateSize++;
|
||||
// Proceed to the next round of selection
|
||||
backtrack(target - choices[i], choices, choicesSize, i + 1);
|
||||
// Backtrack: undo choice, restore to previous state
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Comparison function */
|
||||
int cmp(const void *a, const void *b) {
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
|
||||
/* Solve subset sum II */
|
||||
void subsetSumII(int *nums, int numsSize, int target) {
|
||||
// Sort nums
|
||||
qsort(nums, numsSize, sizeof(int), cmp);
|
||||
// Start backtracking
|
||||
backtrack(target, nums, numsSize, 0);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {4, 4, 5};
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
subsetSumII(nums, numsSize, target);
|
||||
|
||||
printf("Input array nums = ");
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("All subsets with sum equal to %d res = \n", target);
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user