-
Notifications
You must be signed in to change notification settings - Fork 23
/
1. Two Sum.java
executable file
·141 lines (119 loc) · 4.09 KB
/
1. Two Sum.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
E
tags: Array, Hash Table
time: O(n)
space: O(n)
#### HashMap<value, index>
- 相对暴力简洁: 找到一个value, 存一个index
- 若在HashMap里面 match 到结果, 就return HashMap里存的index.
- O(n) space && time.
#### Sort array, two pointer
- 前后++, --搜索. Sort 用时O(nlogn).
- 1. 第一步 two pointer 找 value.
- 2. 注意,要利用额外的空间保留original array, 用来时候找index. (此处不能用HashMap,因为以value 为key,但value可能重复)
- O(n) space, O(nlogn) time.
```
/**
LeetCode: 0-based answer
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
*/
/*
Thoughts:
Using a HashMap, O(n) space and O(n) time.
Thinking process:
Push everything into a HashMap.
Check if one element exist in the HashMap, if so save it. Meanwhile, save the other one.
Trick: after adding into the HashMap, we are looking for the 2nd index first.
Always check (target - current) from the HashMap.
If exist, that means index0 has already been pushed into the HashMap and current value is at index1.
(key, value) = (numbers[i], i)
Note: return index+1 because this is not 0-based.
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] rst = new int[2];
if (nums == null || nums.length <= 1) return rst;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
rst[0] = map.get(target - nums[i]);
rst[1] = i;
break;
}
map.put(nums[i], i);
}
return rst;
}
}
/*
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.
Example
numbers=[2, 7, 11, 15], target=9
return [1, 2]
Note
You may assume that each input would have exactly one solution
Challenge
Either of the following solutions are acceptable:
O(n) Space, O(nlogn) Time
O(n) Space, O(n) Time
Tags Expand
Two Pointers Sort Hash Table Array Airbnb Facebook
*/
//2. O(n) Space O(nlogn) time
/*
Feels like binary search when looking at O(nlogn)
1. sort
2. loop all number
3. binary search on rest
*/
public class Solution {
public int[] twoSum(int[] numbers, int target) {
if (numbers == null || numbers.length == 0) {
return null;
}
int[] original = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
original[i] = numbers[i];
}
Arrays.sort(numbers);
int start = 0;
int end = numbers.length - 1;
int num1 = -1;
int num2 = -1;
while (start != end) {
int sum = numbers[start] + numbers[end];
if (sum == target) {
num1 = numbers[start];
num2 = numbers[end];
break;
}else if (sum < target) {
start++;
} else {
end--;
}
}
//Find the num1,num2 in original array and record the index
int[] rst = new int[2];
rst[0] = -1;
rst[1] = -1;
for (int i = 0; i < original.length; i++) {
if (original[i] == num1 || original[i] == num2) {
if (rst[0] == -1) {
rst[0] = i + 1;
} else {
rst[1] = i + 1;
break;
}
}
}
return rst;
}
}
```