-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo Sum
More file actions
44 lines (31 loc) · 1.01 KB
/
Two Sum
File metadata and controls
44 lines (31 loc) · 1.01 KB
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
36
37
38
39
40
41
42
43
44
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
int[] arr = new int[2];
for (int i = 0; i < nums.length; i++) {
int compliment = target - nums[i];
if (map.containsKey(compliment)) {
arr[0] = i;
arr[1] = map.get(compliment);
break;
}
map.put(nums[i], i);
}
return arr;
}
}
=======================================================================================================================================================================
class Solution {
public int[] twoSum(int[] nums, int target) {
int arr[] = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
arr[0]=i;
arr[1]=j;
}
}
}
return arr;
}
}