Skip to content

Commit 62ddb05

Browse files
committed
Add 中文题0-2000.
1 parent cb2f31d commit 62ddb05

File tree

4,801 files changed

+381486
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,801 files changed

+381486
-0
lines changed
+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# [1. Two Sum](https://leetcode.com/problems/two-sum)
2+
3+
[中文文档](/solution/0000-0099/0001.Two%20Sum/README.md)
4+
5+
## Description
6+
7+
<p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>
8+
9+
<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>
10+
11+
<p>You can return the answer in any order.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [2,7,11,15], target = 9
18+
<strong>Output:</strong> [0,1]
19+
<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
20+
</pre>
21+
22+
<p><strong>Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [3,2,4], target = 6
26+
<strong>Output:</strong> [1,2]
27+
</pre>
28+
29+
<p><strong>Example 3:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [3,3], target = 6
33+
<strong>Output:</strong> [0,1]
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>2 &lt;= nums.length &lt;= 10<sup>3</sup></code></li>
41+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
42+
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
43+
<li><strong>Only one valid answer exists.</strong></li>
44+
</ul>
45+
46+
47+
## Solutions
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
```python
54+
class Solution:
55+
def twoSum(self, nums: List[int], target: int) -> List[int]:
56+
helper = {}
57+
for i, v in enumerate(nums):
58+
num = target - v
59+
if num in helper:
60+
return [helper[num], i]
61+
helper[v] = i
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
class Solution {
68+
public int[] twoSum(int[] nums, int target) {
69+
Map<Integer, Integer> map = new HashMap<>();
70+
for (int i = 0, n = nums.length; i < n; ++i) {
71+
int num = target - nums[i];
72+
if (map.containsKey(num)) {
73+
return new int[]{map.get(num), i};
74+
}
75+
map.put(nums[i], i);
76+
}
77+
return null;
78+
}
79+
}
80+
```
81+
82+
### **JavaScript**
83+
84+
```js
85+
var twoSum = function (nums, target) {
86+
const map = new Map();
87+
for (let i = 0; i < nums.length; i++) {
88+
if (map.has(target - nums[i])) {
89+
return [map.get(target - nums[i]), i];
90+
}
91+
map.set(nums[i], i);
92+
}
93+
return [];
94+
};
95+
```
96+
97+
### **Swift**
98+
99+
```swift
100+
class Solution {
101+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
102+
var map = [Int: Int]()
103+
var i = 0
104+
for num in nums {
105+
map[num] = i
106+
i = i + 1
107+
}
108+
i = 0
109+
for num in nums {
110+
if let otherIndex = map[target - num], otherIndex != i {
111+
return [i, otherIndex]
112+
}
113+
i = i + 1
114+
}
115+
return []
116+
}
117+
}
118+
```
119+
120+
### **Nim**
121+
122+
```nim
123+
import std/enumerate
124+
125+
proc twoSum(nums: seq[int], target: int): seq[int] =
126+
var
127+
bal: int
128+
tdx: int
129+
for idx, val in enumerate(nums):
130+
bal = target - val
131+
if bal in nums:
132+
tdx = nums.find(bal)
133+
if idx != tdx:
134+
return @[idx, tdx]
135+
136+
```
137+
138+
### **...**
139+
140+
```
141+
142+
```
143+
144+
<!-- tabs:end -->

0 commit comments

Comments
 (0)