File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Leetcode/leetcodeTags/array Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ package leetcodeTags .array ;
2+
3+ import java .util .Arrays ;
4+
5+ public class TwoSum1 {
6+
7+ public static int [] twoSum (int [] nums , int target ) {
8+
9+ if (nums .length < 2 )
10+ return null ;
11+
12+ int [] ans = new int [2 ];
13+
14+ for (int i = 0 ; i < nums .length ; i ++) {
15+
16+ int remainingSum = target - nums [i ];
17+
18+ for (int cursor = 0 ; cursor < nums .length ; cursor ++) {
19+
20+ if (cursor != i ) {
21+
22+ if (remainingSum == nums [cursor ]) {
23+ ans [0 ] = i ;
24+ ans [1 ] = cursor ;
25+ }
26+ }
27+ }
28+ }
29+
30+ return ans ;
31+ }
32+
33+ public static void main (String [] args ) {
34+ int [] nums = { 2 , 7 , 11 , 15 };
35+ int target = 9 ;
36+
37+ System .out .println (Arrays .toString (twoSum (nums , target )));
38+ }
39+ }
You canβt perform that action at this time.
0 commit comments