添加了python代码(数组篇)

为数组篇文件夹下的代码增加了python语言版本
This commit is contained in:
goodyong
2021-07-10 12:20:02 +08:00
parent 4e661354d4
commit 7dd5ce1f3d
13 changed files with 461 additions and 3 deletions

View File

@@ -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]
```