-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
perf: 1.c faster implementation of the leetcode TwoSum problem #902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
straight-into-the-wall
wants to merge
20
commits into
TheAlgorithms:master
Choose a base branch
from
straight-into-the-wall:1c
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4ff5849
perf: faster implementation of the TwoSum problem
straight-into-the-wall 6453a9e
doc: fixed typos on comments
straight-into-the-wall 80e54d9
updating DIRECTORY.md
23c0485
fix: comments on includes. Doxygen file. static test function
straight-into-the-wall 13330eb
Merge branch 'master' of github.com:TheAlgorithms/C
straight-into-the-wall e57c16d
fix: revert 1.c
straight-into-the-wall 310c333
updating DIRECTORY.md
3240f54
Merge branch 'master' of github.com:TheAlgorithms/C into 1c
straight-into-the-wall e01e256
perf: faster implementation of the leetcode TwoSum problem
straight-into-the-wall 00985b4
Merge branch 'TheAlgorithms:master' into master
straight-into-the-wall 1f4a876
Merge remote-tracking branch 'upstream/master'
straight-into-the-wall 28d082e
Merge branch 'master' into 1c
straight-into-the-wall 475c735
doc: better comment on two sum test function
straight-into-the-wall d75ebe8
doc: added comment on two sum test function
straight-into-the-wall 16ee7ce
fix: added main and missing comments
straight-into-the-wall ed9f08d
doc: add main comment
straight-into-the-wall 70abeca
fix: file must ends with new line on leetcode/src/1.c
straight-into-the-wall 057eb9e
fix: invalid conversion from (void *) to (int *)
straight-into-the-wall c4d37db
Update leetcode/src/1.c
straight-into-the-wall 1209b17
Update leetcode/src/1.c
straight-into-the-wall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,168 @@ | ||
int *twoSum(int *nums, int numsSize, int target, int *returnSize) | ||
/** | ||
* @file | ||
* @brief TwoSum leetcode problem solution. | ||
* @details | ||
* This is a recursive solution for the leetcode problem of TwoSum. | ||
* @author [straight_into_the_wall](https://github.com/straight-into-the-wall) | ||
* @see https://leetcode.com/problems/two-sum/ | ||
*/ | ||
|
||
#include <assert.h> /// for assert | ||
#include <stdint.h> /// for unsigned int with fixed size | ||
#include <stdlib.h> /// for everithing else | ||
|
||
int* ending(int* a, int* b, int* returnSize); | ||
|
||
/** | ||
* @brief Given an array of integers and an integer target, | ||
* find the two numbers such that they add up to a given target. | ||
* @param nums the array of integer to be processed. | ||
* @param numsSize the size of array on input. | ||
* @param target the integer to be matched. | ||
* @param returnSize a pointer to the size of the output array. | ||
* @returns an array of the indices of the two numbers such that they add up to | ||
* target. | ||
*/ | ||
int* twoSum(int* nums, int numsSize, int target, int* returnSize) | ||
{ | ||
int needle = target - nums[--numsSize]; | ||
|
||
for (int i = numsSize - 1; i >= 0; i--) | ||
if (needle == nums[i]) | ||
return ending(&i, &numsSize, returnSize); | ||
|
||
return twoSum(nums, numsSize, target, returnSize); | ||
} | ||
|
||
/** | ||
* @brief Prepare the output at the end of the execution. Just to keep tidy the | ||
* main function. | ||
* @param a an index of the solution. | ||
* @param b the other index of the solution. | ||
* @param returnSize a pointer to the size of the output array (always 2 on | ||
* success). | ||
* @returns an array of the indices of the two numbers such that they add up to | ||
* target. | ||
*/ | ||
int* ending(int* a, int* b, int* returnSize) | ||
{ | ||
int i, j; | ||
int *ret = calloc(2, sizeof(int)); | ||
for (i = 0; i < numsSize; i++) | ||
{ | ||
int key = target - nums[i]; | ||
for (j = i + 1; j < numsSize; j++) | ||
if (nums[j] == key) | ||
{ | ||
ret[0] = i; | ||
ret[1] = j; | ||
} | ||
} | ||
*returnSize = 2; | ||
int* ret = calloc(*returnSize, sizeof(int)); | ||
ret[0] = *a; | ||
ret[1] = *b; | ||
return ret; | ||
} | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void testTwoSum(int* nums, int numsSize, int target) | ||
{ | ||
int retSize = 0; | ||
int* ret = twoSum(nums, numsSize, target, &retSize); | ||
|
||
for (int i = 0; i < retSize; i++) target -= nums[ret[i]]; | ||
|
||
free(ret); | ||
assert(!target); | ||
} | ||
|
||
static void test(void) | ||
straight-into-the-wall marked this conversation as resolved.
Show resolved
Hide resolved
straight-into-the-wall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
int nums[4] = {2, 7, 15, 11}; | ||
testTwoSum(nums, 4, 9); | ||
|
||
int nums1[4] = {2, -7, 11, 15}; | ||
testTwoSum(nums1, 4, 4); | ||
|
||
int nums2[3] = {3, 2, 4}; | ||
testTwoSum(nums2, 3, 6); | ||
|
||
int nums3[2] = {3, 3}; | ||
testTwoSum(nums3, 2, 6); | ||
|
||
int nums4[3] = {3, -3, 6}; | ||
testTwoSum(nums4, 3, 0); | ||
|
||
int nums5[6] = {-13, 0, 13, 7, -3, 6}; | ||
testTwoSum(nums5, 6, -7); | ||
|
||
int nums6[5] = {-1, -2, -3, -4, -5}; | ||
testTwoSum(nums6, 5, -8); | ||
|
||
int nums7[] = { | ||
1931729, 5896941, -56160, -5999108, -713650, 3135403, 870300, | ||
-2938196, -726482, -152831, -5552046, -6857780, 2493109, -2409591, | ||
7453630, 7848984, -806578, 4976647, -9199319, -7660981, 4168359, | ||
5466164, 8577624, -7465790, -7808993, -7176396, -3622579, 4996177, | ||
9623530, -1371773, -5633028, 5188756, 7971646, -2603602, 7490338, | ||
3174208, -1825054, 6892503, -8201151, -1072749, -2746872, -718104, | ||
-1804706, 5504856, -9104958, -2181797, 3151933, -6116343, 1436458, | ||
-659793, -9256591, -3487143, 3396195, -6211129, -2789647, -9336842, | ||
-122743, -3843085, 8515885, -8551842, -8668581, -5040156, -9626107, | ||
-5645393, -5484621, -6417906, 4483313, -4197219, -5762688, -6645419, | ||
1077632, 7237401, 4819732, 5220382, 8657346, 651862, 7406097, | ||
6668849, 5067699, 4563307, -4358391, -3996666, -4298897, -9870277, | ||
-628517, 1842975, -4970541, -8815324, -4174656, -9332000, 4647373, | ||
9461387, -3504914, 2614670, -1315721, 6099266, -8562834, -4488116, | ||
9742133, 307556, 791291, -2611787, 7656675, 9932436, -1905766, | ||
5142745, 7210001, -4678645, 550797, 8462302, 6815722, -2985059, | ||
-1236401, -6093644, -6617192, -9438320, 8400183, 8936598, 5378080, | ||
9758788, 817106, 8613435, 3579880, -9100698, 993960, 5352546, | ||
8481471, 3526666, 2961295, -7819498, 834324, 3435605, 9725655, | ||
8490290, 7263645, -6863345, 2947003, -8152353, 5514173, -9865666, | ||
343864, -6386758, -8675825, -3252881, 1149651, 6690889, 7528843, | ||
-2861961, -4722247, -6985318, -9994086, 7192089, -4864512, 5773490, | ||
-9492163, -2089496, 2246943, 430576, 3466065, 1578813, 2738295, | ||
261493, -425120, 3666399, 8796329, 9220040, 7148637, 5956722, | ||
9980401, 7834421, 2041136, 3763240, -831391, 7963736, 8744496, | ||
9593751, 4606042, 66708, -6652782, -6619264, 2614784, 5655774, | ||
-9547771, 2456855, 4268450, 864132, 4601358, -3464052, -8987625, | ||
-4427713, -126397, -5823810, 8443040, -7428684, -4365060, -6392451, | ||
-6258724, -9124302, -6566562, -4153846, -9120859, 7061892, -7707777, | ||
9342864, -2133124, -7068189, -8657201, 2752682, -9943634, -8650517, | ||
-5219095, 2305245, -1500154, 8915790, -2805210, 1860598, 3361037, | ||
-8040669, 5809813, -2243400, -3418428, -1810675, -5203397, -7879177, | ||
6898143, 7749161, -2810814, -7525359, -793458, 608571, -9383497, | ||
9531109, 6862130, -3381989, 2332039, 6018975, -3350279, 7266303, | ||
-7170480, -1127784, 578313, 6018329, 8822950, 4214273, -372530, | ||
2706552, -4905224, 3324641, -5403812, 9319424, -2882974, -9228107, | ||
6151855, 2634284, 7000575, 3215247, 5067619, 5076150, 1595871, | ||
748735, -9225927, 1962290, 4653714, 697462, 2996679, -7562085, | ||
-5387705, -912530, 1763635, -6139098, 2103001, -6316004, -3469182, | ||
-3818553, 8854173, 422707, -3826706, -9777012, 7452324, 3665456, | ||
-9458220, 4828502, 2641583, 5107388, -4201437, 5486625, -9315573, | ||
1417427, -1838955, 9042987, -9704280, 7868946, -6737802, 4462332, | ||
-5771637, 635858, 1438260, 8940144, 9385885, 3522022, -9736738, | ||
-4387243, -8611088, 8733735, -2087884, 8299364, -1004025, 2674570, | ||
4247204, 1972151, -2092476, -1318052, -796315, -6487458, 8714867, | ||
-9966767, -5459861, -1286737, 9442201, -2065902, 2575270, 7184243, | ||
5916475, -5728128, -3044939, 6372732, 4902791, 3474374, -7462481, | ||
-9886552, -8361835, -5022392, -4583670, 3990789, -1051830, 8606314, | ||
6365011, 6751820, 1727390, -2005234, -5845824, 9572998, 6867269, | ||
4000416, 5629823, -2918551, 7278467, 9865305, 3781390, -1518411, | ||
-7124219, 3800842, -4492377, -7479001, 4144917, 4639464, 7161451, | ||
3391326, -2429792, 8240315, -8667371, 101661, -6049927, -9729041, | ||
-2210443, -6907416, -1355534, 6292201, -1169114, -4565077, 3214740, | ||
-6122903, 8326559, -3973709, 2313703, 9645383, 6119383, 8317176, | ||
8177731, 7758979, 3037198, -9543017, -3436171, 3280360, -1444577, | ||
-40402, -8372623, 6672469, 9125222, 6161730, 3783090, -2778572, | ||
-8014616, -1588372, -3200391, 7126312, -5091311, -3637052, -6600914, | ||
9034974, -6246289, -4974981, -9711741, -7867786, -9808240, 450272, | ||
-3222991, -8251454, 3599528, -33487, -1114626, 8414308, -2607016, | ||
1690025, 5981016, -2898849, -4650973, -8085619, -4875585, -2677275, | ||
-7329644, -9730701, -5563788, 4635028, -4232606, 1863619, -9081061, | ||
-8966306, 4907349, 348942, -6309867, 2436213, -6340842, 8276407, | ||
-1112458, 4073087, 6632641, -6044509, -1714553, -9905887, 8140713, | ||
-5566484, -8187046, -3636448, 560425, 9931445, -8142363, 1928694, | ||
6652925, 5094575, 5525595, 9847315, 5323311, 9123454, 7425631, | ||
2116547, 9033708, -2228326, 2858537, 5452249, -3388576, 8859366, | ||
5965629, -9904937, 8961420, -8794807, 4363288, -1067547, 1903643, | ||
-6637956, 3736544, 9150157, -3977433, 4333094, -1170635, -4933425, | ||
5815702, 1846616, 793301, -9206787, -227043, -7076926, -461803, | ||
-7477454, -1297664, 47425, 8506054, 8264895, -1199514, -4031592, | ||
1245359, 2840097, 6948011}; | ||
|
||
testTwoSum(nums7, 492, 7828670); | ||
straight-into-the-wall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
straight-into-the-wall marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.