-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (32 loc) · 1 KB
/
Solution.java
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
import java.util.HashMap;
import java.util.Map;
/**
* User: Changle
* Date: 2018-03-11 10:05
* Source: https://leetcode.com/problems/two-sum/description/
*/
/*
因为无序所以不能使用对撞指针,而且需要记录索引值,因此将数据存储在 HashMap 中。
所以先将元素遍历一遍加入到 hashmap 中。第二次遍历时,去 hashmap 中查找 target - nums[i]。
时间复杂度: O(n)
空间复杂度: O(n)
*/
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer,Integer> map = new HashMap();
for(int i = 0;i < nums.length;i++){
map.put(nums[i],i);
}
for(int i = 0;i < nums.length;i++){
if(map.containsKey(target - nums[i])){
if(map.get(target - nums[i]) != i){
res[0] = i;
res[1] = map.get(target - nums[i]);
break;
}
}
}
return res;
}
}