27
27
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
28
28
</ul >
29
29
30
- ## 解法
30
+ ** 方法一:双指针 **
31
31
32
- ** 哈希表 **
32
+ 我们用双指针 $l$ 和 $r$ 分别指向数组的左右两端,然后不断移动指针,直到找到一组和为 $target$ 的连续正整数序列。
33
33
34
- 遍历数组,查看哈希表中是否存在对应的差值(` target ` - 遍历元素):
35
-
36
- - 存在,即 ` return ` 返回。
37
- - 不存在,记录元素,继续遍历。
38
-
39
- _ 复杂度_ :
40
-
41
- - 时间 ** _ O(N)_ **
42
- - 空间 ** _ O(N)_ **
43
-
44
- ** 双指针**
45
-
46
- 1 . 声明头尾指针(数组的左右两端)。
47
- 2 . 将头尾指针所指向的元素相加,与 ` target ` 比较:
48
- - 大于:尾指针前移。
49
- - 小于:头指针后移。
50
- - 等于:返回两个元素即可。
51
- 3 . 重复步骤 2,直到等于为止。
52
-
53
- > 因为数组是有序的,指针变动对值的影响可预测。
54
-
55
- _ 复杂度_ :
56
-
57
- - 时间 ** _ O(N)_ **
58
- - 空间 ** _ O(1)_ **
59
-
60
- ``` txt
61
- TWO-SUM(A,t)
62
- l = 0
63
- r = A.length - 1
64
- while A[l] + A[r] != t
65
- if A[l] + A[r] < t
66
- l = l + 1
67
- else r = r - 1
68
- return [A[l], A[r]]
69
- ```
34
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
70
35
71
36
<!-- tabs:start -->
72
37
73
38
### ** Python3**
74
39
75
- 哈希表:
76
-
77
- ``` python
78
- class Solution :
79
- def twoSum (self , nums : List[int ], target : int ) -> List[int ]:
80
- s = set ()
81
- for num in nums:
82
- if target - num in s:
83
- return [num, target - num]
84
- s.add(num)
85
- ```
86
-
87
- 双指针:
88
-
89
40
``` python
90
41
class Solution :
91
42
def twoSum (self , nums : List[int ], target : int ) -> List[int ]:
92
- p, q = 0 , len (nums) - 1
93
- while p < q:
94
- s = nums[p] + nums[q]
95
- if s == target:
96
- return [nums[p], nums[q]]
97
- if s < target:
98
- p += 1
43
+ l, r = 0 , len (nums) - 1
44
+ while l < r:
45
+ if nums[l] + nums[r] == target:
46
+ return [nums[l], nums[r]]
47
+ if nums[l] + nums[r] > target:
48
+ r -= 1
99
49
else :
100
- q - = 1
50
+ l + = 1
101
51
```
102
52
103
53
### ** Java**
104
54
105
- 哈希表:
106
-
107
- ``` java
108
- class Solution {
109
- public int [] twoSum (int [] nums , int target ) {
110
- Set<Integer > s = new HashSet<> ();
111
- for (int num : nums) {
112
- if (s. contains(target - num)) {
113
- return new int []{num, target - num};
114
- }
115
- s. add(num);
116
- }
117
- return null ;
118
- }
119
- }
120
- ```
121
-
122
- 双指针:
123
-
124
55
``` java
125
56
class Solution {
126
57
public int [] twoSum (int [] nums , int target ) {
127
- for ( int p = 0 , q = nums. length - 1 ; p < q;) {
128
- int s = nums[p] + nums[q];
129
- if (s == target) {
130
- return new int [] {nums[p ], nums[q ]};
58
+ int l = 0 , r = nums. length - 1 ;
59
+ while ( true ) {
60
+ if (nums[l] + nums[r] == target) {
61
+ return new int [] {nums[l ], nums[r ]};
131
62
}
132
- if (s < target) {
133
- ++ p ;
63
+ if (nums[l] + nums[r] > target) {
64
+ -- r ;
134
65
} else {
135
- -- q ;
66
+ ++ l ;
136
67
}
137
68
}
138
- return null ;
139
69
}
140
70
}
141
71
```
@@ -146,18 +76,17 @@ class Solution {
146
76
class Solution {
147
77
public:
148
78
vector<int > twoSum(vector<int >& nums, int target) {
149
- for ( int p = 0, q = nums.size() - 1; p < q;) {
150
- int s = nums [ p ] + nums [ q ] ;
151
- if (s == target) {
152
- return vector< int > {nums[ p ] , nums[ q ] };
79
+ int l = 0, r = nums.size() - 1;
80
+ while (1) {
81
+ if (nums [ l ] + nums [ r ] == target) {
82
+ return {nums[ l ] , nums[ r ] };
153
83
}
154
- if (s < target) {
155
- ++p ;
84
+ if (nums [ l ] + nums [ r ] > target) {
85
+ --r ;
156
86
} else {
157
- --q ;
87
+ ++l ;
158
88
}
159
89
}
160
- return vector<int > {};
161
90
}
162
91
};
163
92
```
@@ -166,18 +95,17 @@ public:
166
95
167
96
```go
168
97
func twoSum(nums []int, target int) []int {
169
- for p, q := 0, len(nums)-1; p < q; {
170
- s := nums[p] + nums[q]
171
- if s == target {
172
- return []int{nums[p ], nums[q ]}
98
+ l, r := 0, len(nums)-1
99
+ for {
100
+ if nums[l]+nums[r] == target {
101
+ return []int{nums[l ], nums[r ]}
173
102
}
174
- if s < target {
175
- p++
103
+ if nums[l]+nums[r] > target {
104
+ r--
176
105
} else {
177
- q--
106
+ l++
178
107
}
179
108
}
180
- return nil
181
109
}
182
110
```
183
111
@@ -190,39 +118,23 @@ func twoSum(nums []int, target int) []int {
190
118
* @return {number[]}
191
119
*/
192
120
var twoSum = function (nums , target ) {
193
- for (let p = 0 , q = nums .length ; p < q; ) {
194
- const s = nums[p] + nums[q];
195
- if (s == target) {
196
- return [nums[p], nums[q]];
121
+ let l = 0 ;
122
+ let r = nums .length - 1 ;
123
+ while (1 ) {
124
+ if (nums[l] + nums[r] == target) {
125
+ return [nums[l], nums[r]];
197
126
}
198
- if (s < target) {
199
- ++ p ;
127
+ if (nums[l] + nums[r] > target) {
128
+ -- r ;
200
129
} else {
201
- -- q ;
130
+ ++ l ;
202
131
}
203
132
}
204
133
};
205
134
```
206
135
207
136
### ** TypeScript**
208
137
209
- 哈希表:
210
-
211
- ``` ts
212
- function twoSum(nums : number [], target : number ): number [] {
213
- const set = new Set ();
214
- for (const num of nums ) {
215
- if (set .has (target - num )) {
216
- return [target - num , num ];
217
- }
218
- set .add (num );
219
- }
220
- return null ;
221
- }
222
- ```
223
-
224
- 双指针:
225
-
226
138
``` ts
227
139
function twoSum(nums : number [], target : number ): number [] {
228
140
let l = 0 ;
@@ -240,8 +152,6 @@ function twoSum(nums: number[], target: number): number[] {
240
152
241
153
### ** Rust**
242
154
243
- 双指针:
244
-
245
155
``` rust
246
156
use std :: cmp :: Ordering ;
247
157
@@ -260,52 +170,22 @@ impl Solution {
260
170
}
261
171
```
262
172
263
- 二分查找:
264
-
265
- ``` rust
266
- use std :: cmp :: Ordering ;
267
-
268
- impl Solution {
269
- pub fn two_sum (nums : Vec <i32 >, target : i32 ) -> Vec <i32 > {
270
- let n = nums . len () - 1 ;
271
- let mut l : usize = 0 ;
272
- let mut r : usize = n ;
273
- for i in 0 .. n {
274
- l = i + 1 ;
275
- r = n ;
276
- let target = target - nums [i ];
277
- while l <= r {
278
- let mid = l + r >> 1 ;
279
- match target . cmp (& nums [mid ]) {
280
- Ordering :: Less => r = mid - 1 ,
281
- Ordering :: Greater => l = mid + 1 ,
282
- Ordering :: Equal => return vec! [nums [i ], nums [mid ]],
283
- }
284
- }
285
- }
286
- vec! [nums [l ], nums [r ]]
287
- }
288
- }
289
- ```
290
-
291
173
### ** C#**
292
174
293
175
``` cs
294
176
public class Solution {
295
177
public int [] TwoSum (int [] nums , int target ) {
296
- int p = 0 , q = nums .Length - 1 ;
297
- while (p < q ) {
298
- int s = nums [p ] + nums [q ];
299
- if (s == target ) {
300
- return new int []{nums [p ], nums [q ]};
178
+ int l = 0 , r = nums .Length - 1 ;
179
+ while (true ) {
180
+ if (nums [l ] + nums [r ] == target ) {
181
+ return new int [] {nums [l ], nums [r ]};
301
182
}
302
- if (s < target ) {
303
- p += 1 ;
183
+ if (nums [ l ] + nums [ r ] > target ) {
184
+ -- r ;
304
185
} else {
305
- q -= 1 ;
186
+ ++ l ;
306
187
}
307
188
}
308
- return new int []{};
309
189
}
310
190
}
311
191
```
0 commit comments