Skip to content

Commit f9b6b9c

Browse files
fix problem 1 add test function
1 parent fd63757 commit f9b6b9c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

C/1-Two-Sum.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <malloc.h>
2-
1+
#include <stdlib.h>
2+
#include
33
/*
44
* Note: The returned array must be malloced, assume caller calls free().
55
* Created by supercoderhawk on 2017/7/20.
@@ -8,13 +8,26 @@
88

99
int* twoSum(int* nums, int numsSize, int target) {
1010
int* indices = (int *)malloc(sizeof(int)*2);
11-
for(int i = 0;i<numsSize-1;i++)
11+
for(int i = 0;i<numsSize;i++)
1212
{
13-
if (nums[i]+nums[i+1]== target)
13+
for(int j = i + 1; j < numsSize; j ++)
1414
{
15-
indices[0] = i;
16-
indices[1] = i + 1;
15+
if (nums[i]+nums[j]== target)
16+
{
17+
indices[0] = i;
18+
indices[1] = j;
19+
}
1720
}
1821
}
1922
return indices;
23+
}
24+
25+
void testTwoSum()
26+
{
27+
const int nums[3] = {3,2,3};
28+
int* indices = twoSum(nums,3,6);
29+
for(int i = 0; i < 2; i ++)
30+
{
31+
printf("%d ", indices[i]);
32+
}
2033
}

0 commit comments

Comments
 (0)