mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加0001.两数之和 Java版本
This commit is contained in:
@ -85,7 +85,24 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
public int[] twoSum(int[] nums, int target) {
|
||||||
|
int[] res = new int[2];
|
||||||
|
if(nums == null || nums.length == 0){
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
Map<Integer, Integer> map = new HashMap<>();
|
||||||
|
for(int i = 0; i < nums.length; i++){
|
||||||
|
int temp = target - nums[i];
|
||||||
|
if(map.containsKey(temp)){
|
||||||
|
res[1] = i;
|
||||||
|
res[0] = map.get(temp);
|
||||||
|
}
|
||||||
|
map.put(nums[i], i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user