@@ -70,11 +70,11 @@ tags:
70
70
71
71
### 方法一:哈希表
72
72
73
- 我们可以用哈希表 $m$ 存放数组值以及对应的下标 。
73
+ 我们可以使用一个哈希表 $\textit{d}$ 来存储每个元素及其对应的索引 。
74
74
75
- 遍历数组 ` nums ` ,当发现 ` target - nums[i] ` 在哈希表中,说明找到了目标值, 返回 ` target - nums[i] ` 的下标以及 $i$ 即可。
75
+ 遍历数组 $\textit{ nums}$,对于当前元素 $\textit{nums} [ i ] $,我们首先判断 $\textit{ target} - \textit{ nums} [ i] $ 是否在哈希表 $\textit{d}$ 中,如果在 $\textit{d}$ 中,说明 $\textit{target}$ 值已经找到, 返回 $\textit{ target} - \textit{ nums} [ i] $ 的索引和 $i$ 即可。
76
76
77
- 时间复杂度 $O(n)$,空间复杂度 $O(n)$。 其中 $n$ 是数组 ` nums ` 的长度。
77
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$, 其中 $n$ 为数组 $\textit{ nums}$ 的长度。
78
78
79
79
<!-- tabs:start -->
80
80
@@ -83,27 +83,27 @@ tags:
83
83
``` python
84
84
class Solution :
85
85
def twoSum (self , nums : List[int ], target : int ) -> List[int ]:
86
- m = {}
86
+ d = {}
87
87
for i, x in enumerate (nums):
88
88
y = target - x
89
- if y in m :
90
- return [m [y], i]
91
- m [x] = i
89
+ if y in d :
90
+ return [d [y], i]
91
+ d [x] = i
92
92
```
93
93
94
94
#### Java
95
95
96
96
``` java
97
97
class Solution {
98
98
public int [] twoSum (int [] nums , int target ) {
99
- Map<Integer , Integer > m = new HashMap<> ();
99
+ Map<Integer , Integer > d = new HashMap<> ();
100
100
for (int i = 0 ;; ++ i) {
101
101
int x = nums[i];
102
102
int y = target - x;
103
- if (m . containsKey(y)) {
104
- return new int [] {m . get(y), i};
103
+ if (d . containsKey(y)) {
104
+ return new int [] {d . get(y), i};
105
105
}
106
- m . put(x, i);
106
+ d . put(x, i);
107
107
}
108
108
}
109
109
}
@@ -115,14 +115,14 @@ class Solution {
115
115
class Solution {
116
116
public:
117
117
vector<int > twoSum(vector<int >& nums, int target) {
118
- unordered_map<int, int> m ;
118
+ unordered_map<int, int> d ;
119
119
for (int i = 0;; ++i) {
120
120
int x = nums[ i] ;
121
121
int y = target - x;
122
- if (m.count (y)) {
123
- return {m [ y] , i};
122
+ if (d.contains (y)) {
123
+ return {d [ y] , i};
124
124
}
125
- m [ x] = i;
125
+ d [ x] = i;
126
126
}
127
127
}
128
128
};
@@ -132,14 +132,14 @@ public:
132
132
133
133
```go
134
134
func twoSum(nums []int, target int) []int {
135
- m := map[int]int{}
135
+ d := map[int]int{}
136
136
for i := 0; ; i++ {
137
137
x := nums[i]
138
138
y := target - x
139
- if j, ok := m [y]; ok {
139
+ if j, ok := d [y]; ok {
140
140
return []int{j, i}
141
141
}
142
- m [x] = i
142
+ d [x] = i
143
143
}
144
144
}
145
145
```
@@ -148,17 +148,14 @@ func twoSum(nums []int, target int) []int {
148
148
149
149
``` ts
150
150
function twoSum(nums : number [], target : number ): number [] {
151
- const m: Map <number , number > = new Map ();
152
-
151
+ const d = new Map <number , number >();
153
152
for (let i = 0 ; ; ++ i ) {
154
153
const x = nums [i ];
155
154
const y = target - x ;
156
-
157
- if (m .has (y )) {
158
- return [m .get (y )! , i ];
155
+ if (d .has (y )) {
156
+ return [d .get (y )! , i ];
159
157
}
160
-
161
- m .set (x , i );
158
+ d .set (x , i );
162
159
}
163
160
}
164
161
```
@@ -170,15 +167,15 @@ use std::collections::HashMap;
170
167
171
168
impl Solution {
172
169
pub fn two_sum (nums : Vec <i32 >, target : i32 ) -> Vec <i32 > {
173
- let mut m = HashMap :: new ();
170
+ let mut d = HashMap :: new ();
174
171
for (i , & x ) in nums . iter (). enumerate () {
175
172
let y = target - x ;
176
- if let Some (& j ) = m . get (& y ) {
173
+ if let Some (& j ) = d . get (& y ) {
177
174
return vec! [j as i32 , i as i32 ];
178
175
}
179
- m . insert (x , i as i32 );
176
+ d . insert (x , i );
180
177
}
181
- unreachable! ()
178
+ vec! []
182
179
}
183
180
}
184
181
```
@@ -192,14 +189,14 @@ impl Solution {
192
189
* @return {number[]}
193
190
*/
194
191
var twoSum = function (nums , target ) {
195
- const m = new Map ();
192
+ const d = new Map ();
196
193
for (let i = 0 ; ; ++ i) {
197
194
const x = nums[i];
198
195
const y = target - x;
199
- if (m .has (y)) {
200
- return [m .get (y), i];
196
+ if (d .has (y)) {
197
+ return [d .get (y), i];
201
198
}
202
- m .set (x, i);
199
+ d .set (x, i);
203
200
}
204
201
};
205
202
```
@@ -209,15 +206,15 @@ var twoSum = function (nums, target) {
209
206
``` cs
210
207
public class Solution {
211
208
public int [] TwoSum (int [] nums , int target ) {
212
- var m = new Dictionary <int , int >();
209
+ var d = new Dictionary <int , int >();
213
210
for (int i = 0 , j ; ; ++ i ) {
214
211
int x = nums [i ];
215
212
int y = target - x ;
216
- if (m .TryGetValue (y , out j )) {
213
+ if (d .TryGetValue (y , out j )) {
217
214
return new [] {j , i };
218
215
}
219
- if (! m .ContainsKey (x )) {
220
- m .Add (x , i );
216
+ if (! d .ContainsKey (x )) {
217
+ d .Add (x , i );
221
218
}
222
219
}
223
220
}
@@ -234,13 +231,13 @@ class Solution {
234
231
* @return Integer[]
235
232
*/
236
233
function twoSum($nums, $target) {
237
- $m = [];
234
+ $d = [];
238
235
foreach ($nums as $i => $x) {
239
236
$y = $target - $x;
240
- if (isset($m [$y])) {
241
- return [$m [$y], $i];
237
+ if (isset($d [$y])) {
238
+ return [$d [$y], $i];
242
239
}
243
- $m [$x] = $i;
240
+ $d [$x] = $i;
244
241
}
245
242
}
246
243
}
@@ -252,17 +249,20 @@ class Solution {
252
249
import scala .collection .mutable
253
250
254
251
object Solution {
255
- def twoSum (nums : Array [Int ], target : Int ): Array [Int ] = {
256
- var map = new mutable.HashMap [Int , Int ]()
257
- for (i <- 0 to nums.length) {
258
- if (map.contains(target - nums(i))) {
259
- return Array (map(target - nums(i)), i)
260
- } else {
261
- map += (nums(i) -> i)
262
- }
252
+ def twoSum (nums : Array [Int ], target : Int ): Array [Int ] = {
253
+ val d = mutable.Map [Int , Int ]()
254
+ var ans : Array [Int ] = Array ()
255
+ for (i <- nums.indices if ans.isEmpty) {
256
+ val x = nums(i)
257
+ val y = target - x
258
+ if (d.contains(y)) {
259
+ ans = Array (d(y), i)
260
+ } else {
261
+ d(x) = i
262
+ }
263
+ }
264
+ ans
263
265
}
264
- Array (0 , 0 )
265
- }
266
266
}
267
267
```
268
268
@@ -271,17 +271,15 @@ object Solution {
271
271
``` swift
272
272
class Solution {
273
273
func twoSum (_ nums : [Int ], _ target : Int ) -> [Int ] {
274
- var m = [Int : Int ]()
275
- var i = 0
276
- while true {
277
- let x = nums[i]
278
- let y = target - nums[i]
279
- if let j = m[target - nums[i]] {
274
+ var d = [Int : Int ]()
275
+ for (i, x) in nums.enumerated () {
276
+ let y = target - x
277
+ if let j = d[y] {
280
278
return [j, i]
281
279
}
282
- m[nums[i]] = i
283
- i += 1
280
+ d[x] = i
284
281
}
282
+ return []
285
283
}
286
284
}
287
285
```
@@ -293,30 +291,31 @@ class Solution {
293
291
# @ param {Integer} target
294
292
# @ return {Integer[]}
295
293
def two_sum (nums , target )
296
- nums.each_with_index do |x , idx |
297
- if nums.include? target - x
298
- return [idx, nums.index(target - x)] if nums.index(target - x) != idx
294
+ d = {}
295
+ nums.each_with_index do |x , i |
296
+ y = target - x
297
+ if d.key?(y)
298
+ return [d[y], i]
299
+ end
300
+ d[x] = i
299
301
end
300
- next
301
- end
302
302
end
303
303
```
304
304
305
305
#### Nim
306
306
307
307
``` nim
308
308
import std/enumerate
309
+ import std/tables
309
310
310
311
proc twoSum(nums: seq[int], target: int): seq[int] =
311
- var
312
- bal: int
313
- tdx: int
314
- for idx, val in enumerate(nums):
315
- bal = target - val
316
- if bal in nums:
317
- tdx = nums.find(bal)
318
- if idx != tdx:
319
- return @[idx, tdx]
312
+ var d = initTable[int, int]()
313
+ for i, x in nums.pairs():
314
+ let y = target - x
315
+ if d.hasKey(y):
316
+ return @[d[y], i]
317
+ d[x] = i
318
+ return @[]
320
319
```
321
320
322
321
<!-- tabs: end -->
0 commit comments