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
31 lines (27 loc) · 781 Bytes
/
S1TwoSum.java
File metadata and controls
31 lines (27 loc) · 781 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
27
28
29
30
31
package Week_02;
import java.util.HashSet;
/**
* @author xingchen.lin
* @desc
* @time 2020/1/12 2:52 下午.
*/
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;
}
}