Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e10f0e

Browse files
authored
feat: Java,python for $1
2 parents 2d10bf4 + c15fe71 commit 5e10f0e

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

problems/1.two-sum.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ for(int i = 0; i < n; i++) {
6565

6666
## 代码
6767

68-
- 语言支持:JS, GoCPP
68+
- 语言支持:JS, Go,CPP,Java,Python
6969

7070
```js
7171
/**
@@ -119,6 +119,36 @@ public:
119119
};
120120
```
121121
122+
Java Code:
123+
124+
```java
125+
class Solution {
126+
public int[] twoSum(int[] nums, int target) {
127+
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
128+
for (int i = 0; i < nums.length; ++i) {
129+
if (hashtable.containsKey(target - nums[i])) {
130+
return new int[]{hashtable.get(target - nums[i]), i};
131+
}
132+
hashtable.put(nums[i], i);
133+
}
134+
return new int[0];
135+
}
136+
}
137+
```
138+
139+
Python Code:
140+
141+
```py
142+
class Solution:
143+
def twoSum(self, nums: List[int], target: int) -> List[int]:
144+
hashtable = dict()
145+
for i, num in enumerate(nums):
146+
if target - num in hashtable:
147+
return [hashtable[target - num], i]
148+
hashtable[nums[i]] = i
149+
return []
150+
```
151+
122152
**复杂度分析**
123153

124154
- 时间复杂度:$O(N)$

0 commit comments

Comments
 (0)