Skip to content

Commit 9a8b4f4

Browse files
authored
Update leetcode1两数之和.md
1 parent 107732a commit 9a8b4f4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

animation-simulation/数组篇/leetcode1两数之和.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Solution {
6969

7070
**题目代码:**
7171

72+
Java Code:
73+
7274
```java
7375
class Solution {
7476
public int[] twoSum(int[] nums, int target) {
@@ -88,5 +90,37 @@ class Solution {
8890
}
8991
```
9092

93+
C++ Code:
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<int> twoSum(vector<int>& nums, int target) {
99+
unordered_map<int, int> m;
100+
for (int i = 0; i < nums.size(); ++i) {
101+
int t = target - nums[i];
102+
if (m.count(t)) return { m[t], i };
103+
m[A[i]] = i;
104+
}
105+
return {};
106+
}
107+
};
108+
```
109+
110+
JS Code:
111+
112+
```js
113+
const twoSum = function (nums, target) {
114+
const map = new Map();
115+
for (let i = 0; i < nums.length; i++) {
116+
const diff = target - nums[i];
117+
if (map.has(diff)) {
118+
return [map.get(diff), i];
119+
}
120+
map.set(nums[i], i);
121+
}
122+
};
123+
```
124+
91125

92126

0 commit comments

Comments
 (0)