-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1_TwoSum.java
More file actions
27 lines (24 loc) · 779 Bytes
/
LC1_TwoSum.java
File metadata and controls
27 lines (24 loc) · 779 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
package practise.leetCode;
import java.util.HashMap;
import java.util.Map;
public class LC1_TwoSum {
public static void main(String[] args) {
int nums[] = {3,3}, target = 6;
int index[] = twoSum(nums, target);
for (int i: index)
System.out.println(i);
}
private static int[] twoSum(int[] nums, int target) {
int rem, i;
Map<Integer, Integer> map = new HashMap<>();
for (i = 0; i < nums.length; i++) {
rem = target - nums[i];
if(map.containsKey(rem)){
// System.out.println("[" + map.get(rem) + "," + i + "]");
return new int[] {map.get(rem), i};
} else
map.put(nums[i], i);
}
return new int[] {};
}
}