forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeegong.java
More file actions
32 lines (26 loc) · 766 Bytes
/
Geegong.java
File metadata and controls
32 lines (26 loc) · 766 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
import java.util.HashMap;
import java.util.Map;
public class Geegong {
/**
* time complexity : O(n)
* space complexity : O(n)
* @param nums
* @param target
* @return int[]
*/
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
// key : value, value = index
Map<Integer, Integer> maps = new HashMap<Integer, Integer>();
for(int index=0; index<nums.length; index++) {
int findOne = target - nums[index];
if (maps.containsKey(findOne)) {
result[0] = maps.get(findOne);
result[1] = index;
return result;
}
maps.put(nums[index], index);
}
return result;
}
}