-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.java
More file actions
26 lines (25 loc) · 830 Bytes
/
Copy pathtwoSum.java
File metadata and controls
26 lines (25 loc) · 830 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
class Solution {
public int[] twoSum(int[] nums, int target) {
// int [] reInt = new int[2];
// for(int i = 0;i<nums.length;i++){
// int tmp = target - nums[i];
// for(int j=i+1;j<nums.length;j++){
// if(tmp == nums[j]){
// reInt[0] = i;
// reInt[1] = j;
// return reInt;
// }
// }
// }
// return reInt;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i<nums.length;i++){
int tmp = target - nums[i];
if(map.containsKey(tmp)){
return new int[] { map.get(tmp), i };
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("No two sum solution");
}
}