Skip to content

Commit 7dd5ce1

Browse files
committed
添加了python代码(数组篇)
为数组篇文件夹下的代码增加了python语言版本
1 parent 4e66135 commit 7dd5ce1

13 files changed

+463
-5
lines changed

animation-simulation/数组篇/leetcode1438绝对值不超过限制的最长子数组.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
其实,我们只要把握两个重点即可,我们的 maxdeque 维护的是一个单调递减的双端队列,头部为当前窗口的最大值, mindeque 维护的是一个单调递增的双端队列,头部为窗口的最小值,即可。好啦我们一起看代码吧。
4545

46+
Java Code:
47+
4648
```java
4749
class Solution {
4850
public int longestSubarray(int[] nums, int limit) {
@@ -76,3 +78,36 @@ class Solution {
7678
}
7779
```
7880

81+
Python Code:
82+
83+
```python
84+
from typing import List
85+
import collections
86+
class Solution:
87+
def longestSubarray(self, nums: List[int], limit: int)->int:
88+
maxdeque = collections.deque()
89+
mindeque = collections.deque()
90+
leng = len(nums)
91+
right = 0
92+
left = 0
93+
maxwin = 0
94+
while right < leng:
95+
while len(maxdeque) != 0 and maxdeque[-1] < nums[right]:
96+
maxdeque.pop()
97+
while len(mindeque) != 0 and mindeque[-1] > nums[right]:
98+
mindeque.pop()
99+
100+
maxdeque.append(nums[right])
101+
mindeque.append(nums[right])
102+
while (maxdeque[0] - mindeque[0]) > limit:
103+
if maxdeque[0] == nums[left]:
104+
maxdeque.popleft()
105+
if mindeque[0] == nums[left]:
106+
mindeque.popleft()
107+
left += 1
108+
# 保留最大窗口
109+
maxwin = max(maxwin, right - left + 1)
110+
right += 1
111+
return maxwin
112+
```
113+

animation-simulation/数组篇/leetcode1两数之和.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
**题目代码**
3535

36+
Java Code:
37+
3638
```java
3739
class Solution {
3840
public int[] twoSum(int[] nums, int target) {
@@ -55,6 +57,25 @@ class Solution {
5557
}
5658
```
5759

60+
Python3 Code:
61+
62+
```python
63+
from typing import List
64+
class Solution:
65+
def twoSum(nums: List[int], target: int)->List[int]:
66+
if len(nums) < 2:
67+
return [0]
68+
rearr = [0] * 2
69+
# 查询元素
70+
for i in range(0, len(nums)):
71+
for j in range(i + 1, len(nums)):
72+
# 发现符合条件情况
73+
if nums[i] + nums[j] == target:
74+
rearr[0] = i
75+
rearr[1] = j
76+
return rearr
77+
```
78+
5879
**哈希表**
5980

6081
**解析**
@@ -122,5 +143,19 @@ const twoSum = function (nums, target) {
122143
};
123144
```
124145

125-
146+
Python3 Code:
147+
148+
```python
149+
from typing import List
150+
class Solution:
151+
def twoSum(self, nums: List[int], target: int)->List[int]:
152+
m = {}
153+
for i in range(0, len(nums)):
154+
# 如果存在则返回
155+
if (target - nums[i]) in m.keys():
156+
return [m[target - nums[i]], i]
157+
# 不存在则存入
158+
m[nums[i]] = i
159+
return [0]
160+
```
126161

animation-simulation/数组篇/leetcode219数组中重复元素2.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
这个题目和我们上面那个数组中的重复数字几乎相同,只不过是增加了一个判断相隔是否小于K位的条件,我们先用 HashMap 来做一哈,和刚才思路一致,我们直接看代码就能整懂
3131

32+
Java Code:
33+
3234
```java
3335
class Solution {
3436
public boolean containsNearbyDuplicate(int[] nums, int k) {
@@ -41,9 +43,9 @@ class Solution {
4143
for (int i = 0; i < nums.length; i++) {
4244
// 如果含有
4345
if (map.containsKey(nums[i])) {
44-
//判断是否小于K,如果小于则直接返回
46+
//判断是否小于K,如果小于等于则直接返回
4547
int abs = Math.abs(i - map.get(nums[i]));
46-
if (abs <= k) return true;//小于则返回
48+
if (abs <= k) return true;//小于等于则返回
4749
}
4850
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
4951
map.put(nums[i],i);
@@ -53,6 +55,29 @@ class Solution {
5355
}
5456
```
5557

58+
Python3 Code:
59+
60+
```python
61+
from typing import List
62+
class Solution:
63+
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
64+
# 特殊情况
65+
if len(nums) == 0:
66+
return False
67+
# 字典
68+
m = {}
69+
for i in range(0, len(nums)):
70+
# 如果含有
71+
if nums[i] in m.keys():
72+
# 判断是否小于K,如果小于等于则直接返回
73+
a = abs(i - m[nums[i]])
74+
if a <= k:
75+
return True# 小于等于则返回
76+
# 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
77+
m[nums[i]] = i
78+
return False
79+
```
80+
5681
**HashSet**
5782

5883
**解析**
@@ -65,6 +90,8 @@ class Solution {
6590

6691
**题目代码**
6792

93+
Java Code
94+
6895
```java
6996
class Solution {
7097
public boolean containsNearbyDuplicate(int[] nums, int k) {
@@ -91,3 +118,25 @@ class Solution {
91118
}
92119
```
93120

121+
Python3 Code:
122+
123+
```python
124+
from typing import List
125+
class Solution:
126+
def containsNearbyDuplicate(self, nums: List[int], k: int)->bool:
127+
# 特殊情况
128+
if len(nums) == 0:
129+
return False
130+
# 集合
131+
s = set()
132+
for i in range(0, len(nums)):
133+
# 如果含有,返回True
134+
if nums[i] in s:
135+
return True
136+
# 添加新元素
137+
s.add(nums[i])
138+
# 维护窗口长度
139+
if len(s) > k:
140+
s.remove(nums[i - k])
141+
return False
142+
```

animation-simulation/数组篇/leetcode27移除元素.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
**题目代码**
5252

53+
Java Code:
54+
5355
```java
5456
class Solution {
5557
public int removeElement(int[] nums, int val) {
@@ -75,6 +77,29 @@ class Solution {
7577
}
7678
```
7779

80+
Python3 Code:
81+
82+
```python
83+
from typing import List
84+
class Solution:
85+
def removeElement(self, nums: List[int], val: int)->int:
86+
# 获取数组长度
87+
leng = len(nums)
88+
if 0 == leng:
89+
return 0
90+
i = 0
91+
while i < leng:
92+
# 发现符合条件的情况
93+
if nums[i] == val:
94+
# 前移一位
95+
for j in range(i, leng - 1):
96+
nums[j] = nums[j + 1]
97+
i -= 1
98+
leng -= 1
99+
i += 1
100+
return i
101+
```
102+
78103
**双指针**
79104

80105
快慢指针的做法比较有趣,只需要一个 for 循环即可解决,时间复杂度为 O(n) ,总体思路就是有两个指针,前面一个后面一个,前面的用于搜索需要删除的值,当遇到需要删除的值时,前指针直接跳过,后面的指针不动,当遇到正常值时,两个指针都进行移动,并修改慢指针的值。最后只需输出慢指针的索引即可。
@@ -100,7 +125,7 @@ class Solution {
100125
if (nums[j] == val) {
101126
continue;
102127
}
103-
// 不等于目标值时,则赋值给num[i],i++
128+
// 不等于目标值时,则赋值给nums[i],i++
104129
nums[i++] = nums[j];
105130
}
106131
return i;

animation-simulation/数组篇/leetcode41缺失的第一个正数.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
![缺失的第一个正数](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/缺失的第一个正数.1it1cow5aa8w.gif)
4141

42+
Java Code:
43+
4244
```java
4345
class Solution {
4446
public int firstMissingPositive(int[] nums) {
@@ -65,6 +67,27 @@ class Solution {
6567
}
6668
```
6769

70+
Python3 Code:
71+
72+
```python
73+
from typing import List
74+
class Solution:
75+
def firstMissingPositive(self, nums: List[int])->int:
76+
if len(nums) == 0:
77+
return 1
78+
# 因为是返回第一个正整数,不包括 0,所以需要长度加1,细节1
79+
res = [0] * (len(nums) + 1)
80+
# 将数组元素添加到辅助数组中
81+
for x in nums:
82+
if x > 0 and x < len(res):
83+
res[x] = x
84+
# 遍历查找,发现不一样时直接返回
85+
for i in range(1, len(res)):
86+
if res[i] != i:
87+
return i
88+
# 缺少最后一个,例如 1,2,3此时缺少 4 ,细节2
89+
return len(res)
90+
```
6891

6992

7093
我们通过上面的例子了解这个解题思想,我们有没有办法不使用辅助数组完成呢?我们可以使用原地置换,直接在 nums 数组内,将值换到对应的索引处,与上个方法思路一致,只不过没有使用辅助数组,理解起来也稍微难理解一些。

animation-simulation/数组篇/leetcode485最大连续1的个数.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
下面我们直接看代码吧
3232

33+
Java Code:
34+
3335
```java
3436
class Solution {
3537
public int findMaxConsecutiveOnes(int[] nums) {
@@ -58,6 +60,29 @@ class Solution {
5860
}
5961
```
6062

63+
Python3 Code:
64+
65+
```python
66+
from typing import List
67+
class Solution:
68+
def findMaxConsecutiveOnes(self, nums: List[int])->int:
69+
leng = len(nums)
70+
left = 0
71+
right = 0
72+
maxcount = 0
73+
while right < leng:
74+
if nums[right] == 1:
75+
right += 1
76+
continue
77+
# 保存最大值
78+
maxcount = max(maxcount, right - left)
79+
# 跳过 0 的情况
80+
while right < leng and nums[right] == 0:
81+
right += 1
82+
# 同一起点继续遍历
83+
left = right
84+
return max(maxcount, right - left)
85+
```
6186

6287

6388
刚才的效率虽然相对高一些,但是代码不够优美,欢迎各位改进,下面我们说一下另外一种情况,一个特别容易理解的方法。

animation-simulation/数组篇/leetcode54螺旋矩阵.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747
题目代码:
4848

49+
Java Code:
50+
4951
```java
5052
class Solution {
5153
public List<Integer> spiralOrder(int[][] matrix) {
@@ -83,5 +85,40 @@ class Solution {
8385

8486
```
8587

88+
Python3 Code:
89+
90+
```python
91+
from typing import List
92+
class Solution:
93+
def spiralOrder(self, matrix: List[List[int]])->List[int]:
94+
arr = []
95+
left = 0
96+
right = len(matrix[0]) - 1
97+
top = 0
98+
down = len(matrix) - 1
99+
while True:
100+
for i in range(left, right + 1):
101+
arr.append(matrix[top][i])
102+
top += 1
103+
if top > down:
104+
break
105+
for i in range(top, down + 1):
106+
arr.append(matrix[i][right])
107+
right -= 1
108+
if left > right:
109+
break
110+
for i in range(right, left - 1, -1):
111+
arr.append(matrix[down][i])
112+
down -= 1
113+
if top > down:
114+
break
115+
for i in range(down, top - 1, -1):
116+
arr.append(matrix[i][left])
117+
left += 1
118+
if left > right:
119+
break
120+
return arr
121+
```
122+
86123

87124

0 commit comments

Comments
 (0)