-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
39 lines (36 loc) · 1000 Bytes
/
Copy pathTwoSum.java
File metadata and controls
39 lines (36 loc) · 1000 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
36
37
38
39
package leetcode;
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] output = new int[2];
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
for (int i = 0 ; i < nums.length ; i++) {
if (map.containsKey(target - nums[i])) {
output[1] = i;
output[0] = map.get(target - nums[i])
return output;
}
map.put(nums[i], i);
}
return output;
// int index1 = 0;
// int index2 = 0;
// int output[] = { index1, index2 };
//
// for (int i = 0; i <nums.length ; i++){
// int Iterate1 = nums[i];
//
// for (int j = 0; j <nums.length ; j++){
// int Iterate2 = nums[j];
// if (Iterate1 + Iterate2 == target) {
//
// index1 = i;
// index2 = j;
// return output;
// }
// }
// }
// return output;
}
}