diff --git "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" index 5bedd0a0af..bc618c1506 100644 --- "a/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" +++ "b/problems/0001.344円270円244円346円225円260円344円271円213円345円222円214円.md" @@ -352,5 +352,86 @@ List twoSum(List nums, int target) { } ``` +C: +```c + + +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ + +// leetcode 支持 ut_hash 函式庫 + + typedef struct { + int key; + int value; + UT_hash_handle hh; // make this structure hashable + } map; + +map* hashMap = NULL; + + void hashMapAdd(int key, int value){ + map* s; + // key already in the hash? + HASH_FIND_INT(hashMap, &key, s); + if(s == NULL){ + s = (map*)malloc(sizeof(map)); + s -> key = key; + HASH_ADD_INT(hashMap, key, s); + } + s -> value = value; + } + +map* hashMapFind(int key){ + map* s; + // *s: output pointer + HASH_FIND_INT(hashMap, &key, s); + return s; + } + + void hashMapCleanup(){ + map* cur, *tmp; + HASH_ITER(hh, hashMap, cur, tmp){ + HASH_DEL(hashMap, cur); + free(cur); + } + } + + void hashPrint(){ + map* s; + for(s = hashMap; s != NULL; s=(map*)(s -> hh.next)){ + printf("key %d, value %d\n", s -> key, s -> value); + } + } + + +int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + int i, *ans; + // hash find result + map* hashMapRes; + hashMap = NULL; + ans = malloc(sizeof(int) * 2); + + for(i = 0; i < numsSize; i++){ + // key 代表 nums[i] 的值,value 代表所在 index; + hashMapAdd(nums[i], i); + } + + hashPrint(); + + for(i = 0; i < numsSize; i++){ + hashMapRes = hashMapFind(target - nums[i]); + if(hashMapRes && hashMapRes -> value != i){ + ans[0] = i; + ans[1] = hashMapRes -> value ; + *returnSize = 2; + return ans; + } + } + + hashMapCleanup(); + return NULL; +} +``` -----------------------
diff --git "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" index b9859968c3..f84e334e1f 100644 --- "a/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" +++ "b/problems/0019.345円210円240円351円231円244円351円223円276円350円241円250円347円232円204円345円200円222円346円225円260円347円254円254円N344円270円252円350円212円202円347円202円271円.md" @@ -75,7 +75,12 @@ public: fast = fast->next; slow = slow->next; } - slow->next = slow->next->next; + slow->next = slow->next->next; + + // ListNode *tmp = slow->next; C++释放内存的逻辑 + // slow->next = tmp->next; + // delete nth; + return dummyHead->next; } };

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