添加了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

@@ -29,6 +29,8 @@
这个题目和我们上面那个数组中的重复数字几乎相同只不过是增加了一个判断相隔是否小于K位的条件我们先用 HashMap 来做一哈和刚才思路一致我们直接看代码就能整懂
Java Code:
```java
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
@@ -41,9 +43,9 @@ class Solution {
for (int i = 0; i < nums.length; i++) {
// 如果含有
if (map.containsKey(nums[i])) {
//判断是否小于K如果小于则直接返回
//判断是否小于K如果小于等于则直接返回
int abs = Math.abs(i - map.get(nums[i]));
if (abs <= k) return true;//小于则返回
if (abs <= k) return true;//小于等于则返回
}
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
map.put(nums[i],i);
@@ -53,6 +55,29 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
# 特殊情况
if len(nums) == 0:
return False
# 字典
m = {}
for i in range(0, len(nums)):
# 如果含有
if nums[i] in m.keys():
# 判断是否小于K如果小于等于则直接返回
a = abs(i - m[nums[i]])
if a <= k:
return True# 小于等于则返回
# 更新索引此时有两种情况不存在或者存在时将后出现的索引保存
m[nums[i]] = i
return False
```
**HashSet**
**解析**
@@ -65,6 +90,8 @@ class Solution {
**题目代码**
Java Code
```java
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
@@ -91,3 +118,25 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
# 特殊情况
if len(nums) == 0:
return False
# 集合
s = set()
for i in range(0, len(nums)):
# 如果含有返回True
if nums[i] in s:
return True
# 添加新元素
s.add(nums[i])
# 维护窗口长度
if len(s) > k:
s.remove(nums[i - k])
return False
```