forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0001TwoSum.java
More file actions
44 lines (37 loc) ยท 1.04 KB
/
_0001TwoSum.java
File metadata and controls
44 lines (37 loc) ยท 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package leetcodeEasyLevel;
import java.util.Arrays;
public class _0001TwoSum {
public static void main(String[] args) {
int[] nums;
int target;
//test case1
//nums = new int[]{3, 2, 4};
//target = 6;
//test case2
nums = new int[]{2, 7, 11, 15};
target = 9;
int[] ans = new int[2];
for(int i=0; i<nums.length; i++){
for(int j=i+1; j<nums.length; j++){
if(nums[i]+nums[j] == target){
ans[0] = i;
ans[1] = j;
break;
}
}
}
//return ans;
System.out.println(Arrays.toString(ans));
//solution
/*
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
*/
}
}