Skip to content

Commit e5b5d95

Browse files
authoredAug 9, 2024··
feat: update solutions to lc problem: No.0001 (#3392)
1 parent d5f8fd4 commit e5b5d95

15 files changed

+217
-220
lines changed
 

‎solution/0000-0099/0001.Two Sum/README.md

+72-73
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ tags:
7070

7171
### 方法一:哈希表
7272

73-
我们可以用哈希表 $m$ 存放数组值以及对应的下标
73+
我们可以使用一个哈希表 $\textit{d}$ 来存储每个元素及其对应的索引
7474

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$ 即可。
7676

77-
时间复杂度 $O(n)$,空间复杂度 $O(n)$其中 $n$ 是数组 `nums` 的长度。
77+
时间复杂度 $O(n)$,空间复杂度 $O(n)$其中 $n$ 为数组 $\textit{nums}$ 的长度。
7878

7979
<!-- tabs:start -->
8080

@@ -83,27 +83,27 @@ tags:
8383
```python
8484
class Solution:
8585
def twoSum(self, nums: List[int], target: int) -> List[int]:
86-
m = {}
86+
d = {}
8787
for i, x in enumerate(nums):
8888
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
9292
```
9393

9494
#### Java
9595

9696
```java
9797
class Solution {
9898
public int[] twoSum(int[] nums, int target) {
99-
Map<Integer, Integer> m = new HashMap<>();
99+
Map<Integer, Integer> d = new HashMap<>();
100100
for (int i = 0;; ++i) {
101101
int x = nums[i];
102102
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};
105105
}
106-
m.put(x, i);
106+
d.put(x, i);
107107
}
108108
}
109109
}
@@ -115,14 +115,14 @@ class Solution {
115115
class Solution {
116116
public:
117117
vector<int> twoSum(vector<int>& nums, int target) {
118-
unordered_map<int, int> m;
118+
unordered_map<int, int> d;
119119
for (int i = 0;; ++i) {
120120
int x = nums[i];
121121
int y = target - x;
122-
if (m.count(y)) {
123-
return {m[y], i};
122+
if (d.contains(y)) {
123+
return {d[y], i};
124124
}
125-
m[x] = i;
125+
d[x] = i;
126126
}
127127
}
128128
};
@@ -132,14 +132,14 @@ public:
132132
133133
```go
134134
func twoSum(nums []int, target int) []int {
135-
m := map[int]int{}
135+
d := map[int]int{}
136136
for i := 0; ; i++ {
137137
x := nums[i]
138138
y := target - x
139-
if j, ok := m[y]; ok {
139+
if j, ok := d[y]; ok {
140140
return []int{j, i}
141141
}
142-
m[x] = i
142+
d[x] = i
143143
}
144144
}
145145
```
@@ -148,17 +148,14 @@ func twoSum(nums []int, target int) []int {
148148

149149
```ts
150150
function twoSum(nums: number[], target: number): number[] {
151-
const m: Map<number, number> = new Map();
152-
151+
const d = new Map<number, number>();
153152
for (let i = 0; ; ++i) {
154153
const x = nums[i];
155154
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];
159157
}
160-
161-
m.set(x, i);
158+
d.set(x, i);
162159
}
163160
}
164161
```
@@ -170,15 +167,15 @@ use std::collections::HashMap;
170167

171168
impl Solution {
172169
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
173-
let mut m = HashMap::new();
170+
let mut d = HashMap::new();
174171
for (i, &x) in nums.iter().enumerate() {
175172
let y = target - x;
176-
if let Some(&j) = m.get(&y) {
173+
if let Some(&j) = d.get(&y) {
177174
return vec![j as i32, i as i32];
178175
}
179-
m.insert(x, i as i32);
176+
d.insert(x, i);
180177
}
181-
unreachable!()
178+
vec![]
182179
}
183180
}
184181
```
@@ -192,14 +189,14 @@ impl Solution {
192189
* @return {number[]}
193190
*/
194191
var twoSum = function (nums, target) {
195-
const m = new Map();
192+
const d = new Map();
196193
for (let i = 0; ; ++i) {
197194
const x = nums[i];
198195
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];
201198
}
202-
m.set(x, i);
199+
d.set(x, i);
203200
}
204201
};
205202
```
@@ -209,15 +206,15 @@ var twoSum = function (nums, target) {
209206
```cs
210207
public class Solution {
211208
public int[] TwoSum(int[] nums, int target) {
212-
var m = new Dictionary<int, int>();
209+
var d = new Dictionary<int, int>();
213210
for (int i = 0, j; ; ++i) {
214211
int x = nums[i];
215212
int y = target - x;
216-
if (m.TryGetValue(y, out j)) {
213+
if (d.TryGetValue(y, out j)) {
217214
return new [] {j, i};
218215
}
219-
if (!m.ContainsKey(x)) {
220-
m.Add(x, i);
216+
if (!d.ContainsKey(x)) {
217+
d.Add(x, i);
221218
}
222219
}
223220
}
@@ -234,13 +231,13 @@ class Solution {
234231
* @return Integer[]
235232
*/
236233
function twoSum($nums, $target) {
237-
$m = [];
234+
$d = [];
238235
foreach ($nums as $i => $x) {
239236
$y = $target - $x;
240-
if (isset($m[$y])) {
241-
return [$m[$y], $i];
237+
if (isset($d[$y])) {
238+
return [$d[$y], $i];
242239
}
243-
$m[$x] = $i;
240+
$d[$x] = $i;
244241
}
245242
}
246243
}
@@ -252,17 +249,20 @@ class Solution {
252249
import scala.collection.mutable
253250

254251
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
263265
}
264-
Array(0, 0)
265-
}
266266
}
267267
```
268268

@@ -271,17 +271,15 @@ object Solution {
271271
```swift
272272
class Solution {
273273
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] {
280278
return [j, i]
281279
}
282-
m[nums[i]] = i
283-
i += 1
280+
d[x] = i
284281
}
282+
return []
285283
}
286284
}
287285
```
@@ -293,30 +291,31 @@ class Solution {
293291
# @param {Integer} target
294292
# @return {Integer[]}
295293
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
299301
end
300-
next
301-
end
302302
end
303303
```
304304

305305
#### Nim
306306

307307
```nim
308308
import std/enumerate
309+
import std/tables
309310
310311
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 @[]
320319
```
321320

322321
<!-- tabs:end -->

0 commit comments

Comments
 (0)
Please sign in to comment.