-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01TwoSum.java
More file actions
35 lines (32 loc) · 935 Bytes
/
01TwoSum.java
File metadata and controls
35 lines (32 loc) · 935 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
32
33
34
35
class Solution1 {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int a = 0; a < nums.length; a++){
if(a == i){
continue;
}
if (nums[a] + nums[i] == target){
int[] temp = {i,a};
return temp;
}
}
}
return null;
}
}
import java.util.HashMap;
class Solution2 {
public int[] twoSum(int[] nums, int target){
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0; i < nums.length; i++){
int wish = target - nums[i];
if(map.get(wish) != null){
return new int[] {i,map.get(wish)};
}
if(map.get(nums[i]) == null){
map.put(nums[i],i);
}
}
return null;
}
}