mirror of
http://bgp.hk.skcks.cn:10086/https://github.com/krahets/hello-algo
synced 2026-04-20 21:00:58 +08:00
Some checks failed
C / build (Release, cl, codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, cl, en/codes/c, cl, windows-latest) (push) Has been cancelled
C / build (Release, clang, codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, clang, en/codes/c, clang++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, codes/c, g++, ubuntu-latest) (push) Has been cancelled
C / build (Release, gcc, en/codes/c, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, cl, codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, cl, en/codes/cpp, cl, windows-latest) (push) Has been cancelled
C++ / build (Release, clang, codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, clang, en/codes/cpp, clang++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
C++ / build (Release, gcc, en/codes/cpp, g++, ubuntu-latest) (push) Has been cancelled
Dart / Dart stable on macos-latest (push) Has been cancelled
Dart / Dart stable on ubuntu-latest (push) Has been cancelled
Dart / Dart stable on windows-latest (push) Has been cancelled
.NET / .NET 8.0.x on macos-latest (push) Has been cancelled
.NET / .NET 8.0.x on ubuntu-latest (push) Has been cancelled
.NET / .NET 8.0.x on windows-latest (push) Has been cancelled
Go / Go 1.19.x on macos-latest (push) Has been cancelled
Go / Go 1.19.x on ubuntu-latest (push) Has been cancelled
Go / Go 1.19.x on windows-latest (push) Has been cancelled
Java / Java 11 sample (push) Has been cancelled
Java / Java 17 sample (push) Has been cancelled
JavaScript / build (codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (codes/javascript, windows-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, macos-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, ubuntu-latest) (push) Has been cancelled
JavaScript / build (en/codes/javascript, windows-latest) (push) Has been cancelled
Kotlin / Kotlin on macos-latest (push) Has been cancelled
Kotlin / Kotlin on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on macos-latest (push) Has been cancelled
Python / Python 3.10 on ubuntu-latest (push) Has been cancelled
Python / Python 3.10 on windows-latest (push) Has been cancelled
Ruby / Ruby 3.3 on macos-latest (push) Has been cancelled
Ruby / Ruby 3.3 on ubuntu-latest (push) Has been cancelled
Ruby / Ruby 3.3 on windows-latest (push) Has been cancelled
Rust / build (codes/rust, macos-latest) (push) Has been cancelled
Rust / build (codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (codes/rust, windows-latest) (push) Has been cancelled
Rust / build (en/codes/rust, macos-latest) (push) Has been cancelled
Rust / build (en/codes/rust, ubuntu-latest) (push) Has been cancelled
Rust / build (en/codes/rust, windows-latest) (push) Has been cancelled
Swift / Swift on macos-14 (push) Has been cancelled
Swift / Swift on ubuntu-22.04 (push) Has been cancelled
TypeScript / build (codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (codes/typescript, windows-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, macos-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, ubuntu-latest) (push) Has been cancelled
TypeScript / build (en/codes/typescript, windows-latest) (push) Has been cancelled
* 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
86 lines
2.2 KiB
C
86 lines
2.2 KiB
C
/**
|
|
* File: two_sum.c
|
|
* Created Time: 2023-01-19
|
|
* Author: Reanon (793584285@qq.com)
|
|
*/
|
|
|
|
#include "../utils/common.h"
|
|
|
|
/* Method 1: Brute force enumeration */
|
|
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
|
|
for (int i = 0; i < numsSize; ++i) {
|
|
for (int j = i + 1; j < numsSize; ++j) {
|
|
if (nums[i] + nums[j] == target) {
|
|
int *res = malloc(sizeof(int) * 2);
|
|
res[0] = i, res[1] = j;
|
|
*returnSize = 2;
|
|
return res;
|
|
}
|
|
}
|
|
}
|
|
*returnSize = 0;
|
|
return NULL;
|
|
}
|
|
|
|
/* Hash table */
|
|
typedef struct {
|
|
int key;
|
|
int val;
|
|
UT_hash_handle hh; // Implemented using uthash.h
|
|
} HashTable;
|
|
|
|
/* Hash table lookup */
|
|
HashTable *find(HashTable *h, int key) {
|
|
HashTable *tmp;
|
|
HASH_FIND_INT(h, &key, tmp);
|
|
return tmp;
|
|
}
|
|
|
|
/* Hash table element insertion */
|
|
void insert(HashTable **h, int key, int val) {
|
|
HashTable *t = find(*h, key);
|
|
if (t == NULL) {
|
|
HashTable *tmp = malloc(sizeof(HashTable));
|
|
tmp->key = key, tmp->val = val;
|
|
HASH_ADD_INT(*h, key, tmp);
|
|
} else {
|
|
t->val = val;
|
|
}
|
|
}
|
|
|
|
/* Method 2: Auxiliary hash table */
|
|
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
|
HashTable *hashtable = NULL;
|
|
for (int i = 0; i < numsSize; i++) {
|
|
HashTable *t = find(hashtable, target - nums[i]);
|
|
if (t != NULL) {
|
|
int *res = malloc(sizeof(int) * 2);
|
|
res[0] = t->val, res[1] = i;
|
|
*returnSize = 2;
|
|
return res;
|
|
}
|
|
insert(&hashtable, nums[i], i);
|
|
}
|
|
*returnSize = 0;
|
|
return NULL;
|
|
}
|
|
|
|
/* Driver Code */
|
|
int main() {
|
|
// ======= Test Case =======
|
|
int nums[] = {2, 7, 11, 15};
|
|
int target = 13;
|
|
// ====== Driver Code ======
|
|
int returnSize;
|
|
int *res = twoSumBruteForce(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
|
// Method 1
|
|
printf("Method 1 res = ");
|
|
printArray(res, returnSize);
|
|
|
|
// Method 2
|
|
res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
|
printf("Method 2 res = ");
|
|
printArray(res, returnSize);
|
|
|
|
return 0;
|
|
} |