mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 21:01:08 +00:00
添加了python代码(数组篇)
为数组篇文件夹下的代码增加了python语言版本
This commit is contained in:
@@ -33,6 +33,8 @@
|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
@@ -55,6 +57,25 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python3 Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def twoSum(nums: List[int], target: int)->List[int]:
|
||||
if len(nums) < 2:
|
||||
return [0]
|
||||
rearr = [0] * 2
|
||||
# 查询元素
|
||||
for i in range(0, len(nums)):
|
||||
for j in range(i + 1, len(nums)):
|
||||
# 发现符合条件情况
|
||||
if nums[i] + nums[j] == target:
|
||||
rearr[0] = i
|
||||
rearr[1] = j
|
||||
return rearr
|
||||
```
|
||||
|
||||
**哈希表**
|
||||
|
||||
**解析**
|
||||
@@ -122,5 +143,19 @@ const twoSum = function (nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
Python3 Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int)->List[int]:
|
||||
m = {}
|
||||
for i in range(0, len(nums)):
|
||||
# 如果存在则返回
|
||||
if (target - nums[i]) in m.keys():
|
||||
return [m[target - nums[i]], i]
|
||||
# 不存在则存入
|
||||
m[nums[i]] = i
|
||||
return [0]
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user