-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathTwoSum.java
More file actions
89 lines (59 loc) · 2.16 KB
/
TwoSum.java
File metadata and controls
89 lines (59 loc) · 2.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package oj.leetcode;
import java.util.HashMap;
import java.util.Map;
/*
* Given an array of integers, find two numbers such that they add up
* to a specific target number.
* The function twoSum should return indices of the two numbers
* such that they add up to the target, where index1 must be less than index2.
* Please note that your returned answers (both index1 and index2) are not zero-based.
*
* You may assume that each input would have exactly one solution.
* Input: numbers={2, 7, 11, 15}, target=9
* Output: index1=1, index2=2
*
*
*/
public class TwoSum {
//AC
//1. 最自然的方法 O(n^2)
public int[] twoSum1(int[] numbers, int target) {
if (numbers == null || numbers.length <= 1)
return null;
int idx1 = 0, idx2 = 0;
labelA:
// 指定标签,跳出最外层循环
for (idx1 = 0; idx1 < numbers.length - 1; idx1++)
for (idx2 = idx1 + 1; idx2 < numbers.length; idx2++) {
if ((numbers[idx1] + numbers[idx2]) == target)
break labelA;
}
if (idx2 >= numbers.length)
return null;
return new int[]{idx1 + 1, idx2 + 1};
}
//AC
// 2. 换一个角度看问题,遍历到x,看sum - x 是否存在,就会想到利用哈希表
//特别注意的是处理重复元素的情况,所以不能先全部hash进去,再查找,而是动态判定
public int[] twoSum(int[] numbers, int target) {
if (numbers == null || numbers.length <= 1)
return null;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(numbers[i])) {
int a = map.get(numbers[i]) + 1;
int b = i + 1;
return new int[]{a, b};
} else {
map.put(target - numbers[i], i);
}
}
return null;
}
public static void main(String[] args) {
int[] nums = new int[]{2, 7, 11, 15};
int target = 9;
int[] res = new TwoSum().twoSum(nums, target);
Util.printArr(res);
}
}