forked from algorithm008-class02/algorithm008-class02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS1TwoSum.java
More file actions
26 lines (22 loc) · 708 Bytes
/
S1TwoSum.java
File metadata and controls
26 lines (22 loc) · 708 Bytes
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
package Week_01;
import java.util.HashSet;
public class S1TwoSum {
public static int[] twoSum(int[] nums, int target) {
HashSet<Integer> hashSet = new HashSet<>();
for (int num: nums) {
hashSet.add(num);
}
for (int i = 0; i < nums.length; i++) {
if (hashSet.contains(target - nums[i])) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == target - nums[i] && j != i) {
System.out.println(i);
System.out.println(j);
return new int[]{i, j};
}
}
}
}
return null;
}
}