File tree Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Expand file tree Collapse file tree 1 file changed +19
-6
lines changed Original file line number Diff line number Diff line change 1
- #include <malloc .h>
2
-
1
+ #include <stdlib .h>
2
+ #include
3
3
/*
4
4
* Note: The returned array must be malloced, assume caller calls free().
5
5
* Created by supercoderhawk on 2017/7/20.
8
8
9
9
int * twoSum (int * nums , int numsSize , int target ) {
10
10
int * indices = (int * )malloc (sizeof (int )* 2 );
11
- for (int i = 0 ;i < numsSize - 1 ;i ++ )
11
+ for (int i = 0 ;i < numsSize ;i ++ )
12
12
{
13
- if ( nums [ i ] + nums [ i + 1 ] == target )
13
+ for ( int j = i + 1 ; j < numsSize ; j ++ )
14
14
{
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
+ }
17
20
}
18
21
}
19
22
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
+ }
20
33
}
You can’t perform that action at this time.
0 commit comments