-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution01.java
More file actions
49 lines (43 loc) · 1.08 KB
/
Copy pathSolution01.java
File metadata and controls
49 lines (43 loc) · 1.08 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
45
46
47
48
49
/**
* @Title: Solution01.java——
* @Package EasyCode
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月26日 下午2:51:49
* @version V1.0
*/
package EasyCode;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: Solution01——两数之和
* @Description: TODO
* @author msdumin@gmail.com
* @date 2019年3月26日 下午2:51:49
*/
public class Solution01 {
//暴力循环法:O(n2)
//排序 + 对撞指针 O(nlogn) + O(n)
//查找表
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complex = target - nums[i];
if(map.containsKey(complex)){
int[] res = {i , map.get(complex)};
return res;
}
map.put(nums[i], i);
}
throw new IllegalStateException("no solution!");
}
private static void pritArr(int[] nums){
for(int num : nums)
System.out.print(num + " ");
}
public static void main(String[] args) {
int nums[] = {0,4,3,0};
int target = 0;
pritArr(new Solution01().twoSum(nums, target));
}
}