添加了python代码(数组篇)

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

View File

@ -43,6 +43,8 @@
maxdeque mindeque
Java Code:
```java
class Solution {
public int longestSubarray(int[] nums, int limit) {
@ -76,3 +78,36 @@ class Solution {
}
```
Python Code:
```python
from typing import List
import collections
class Solution:
def longestSubarray(self, nums: List[int], limit: int)->int:
maxdeque = collections.deque()
mindeque = collections.deque()
leng = len(nums)
right = 0
left = 0
maxwin = 0
while right < leng:
while len(maxdeque) != 0 and maxdeque[-1] < nums[right]:
maxdeque.pop()
while len(mindeque) != 0 and mindeque[-1] > nums[right]:
mindeque.pop()
maxdeque.append(nums[right])
mindeque.append(nums[right])
while (maxdeque[0] - mindeque[0]) > limit:
if maxdeque[0] == nums[left]:
maxdeque.popleft()
if mindeque[0] == nums[left]:
mindeque.popleft()
left += 1
#
maxwin = max(maxwin, right - left + 1)
right += 1
return maxwin
```

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

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

View File

@ -50,6 +50,8 @@
****
Java Code:
```java
class Solution {
public int removeElement(int[] nums, int val) {
@ -75,6 +77,29 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int)->int:
#
leng = len(nums)
if 0 == leng:
return 0
i = 0
while i < leng:
#
if nums[i] == val:
#
for j in range(i, leng - 1):
nums[j] = nums[j + 1]
i -= 1
leng -= 1
i += 1
return i
```
****
for O(n) ,
@ -100,7 +125,7 @@ class Solution {
if (nums[j] == val) {
continue;
}
// 不等于目标值时则赋值给num[i],i++
// 不等于目标值时则赋值给nums[i],i++
nums[i++] = nums[j];
}
return i;

View File

@ -39,6 +39,8 @@
![](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/缺失的第一个正数.1it1cow5aa8w.gif)
Java Code:
```java
class Solution {
public int firstMissingPositive(int[] nums) {
@ -65,6 +67,27 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def firstMissingPositive(self, nums: List[int])->int:
if len(nums) == 0:
return 1
# 011
res = [0] * (len(nums) + 1)
#
for x in nums:
if x > 0 and x < len(res):
res[x] = x
# ,
for i in range(1, len(res)):
if res[i] != i:
return i
# 123 4 2
return len(res)
```
使使 nums 使

View File

@ -30,6 +30,8 @@
Java Code:
```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
@ -58,6 +60,29 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int])->int:
leng = len(nums)
left = 0
right = 0
maxcount = 0
while right < leng:
if nums[right] == 1:
right += 1
continue
#
maxcount = max(maxcount, right - left)
# 0
while right < leng and nums[right] == 0:
right += 1
#
left = right
return max(maxcount, right - left)
```

View File

@ -46,6 +46,8 @@
Java Code:
```java
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
@ -83,5 +85,40 @@ class Solution {
```
Python3 Code:
```python
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]])->List[int]:
arr = []
left = 0
right = len(matrix[0]) - 1
top = 0
down = len(matrix) - 1
while True:
for i in range(left, right + 1):
arr.append(matrix[top][i])
top += 1
if top > down:
break
for i in range(top, down + 1):
arr.append(matrix[i][right])
right -= 1
if left > right:
break
for i in range(right, left - 1, -1):
arr.append(matrix[down][i])
down -= 1
if top > down:
break
for i in range(down, top - 1, -1):
arr.append(matrix[i][left])
left += 1
if left > right:
break
return arr
```

View File

@ -21,6 +21,8 @@
k
Java Code:
```java
class Solution {
public int subarraySum(int[] nums, int k) {
@ -41,6 +43,8 @@ class Solution {
}
```
Python3
使西
![](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/微信截图_20210113193831.4wk2b9zc8vm0.png)
@ -61,6 +65,8 @@ presum [2] = presum[1] + nums[1],presum[3] = presum[2] + nums[2] ... 所以我
Java Code:
```java
class Solution {
public int subarraySum(int[] nums, int k) {
@ -86,6 +92,8 @@ class Solution {
}
```
Python3
** + HashMap**
@ -94,6 +102,8 @@ class Solution {
HashMap .
Java Code:
```java
class Solution {
public int[] twoSum(int[] nums, int target) {

View File

@ -66,6 +66,8 @@
Java Code:
```java
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
@ -103,10 +105,45 @@ class Solution {
```
Python3 Code:
```python
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]])->List[int]:
arr = []
left = 0
right = len(matrix[0]) - 1
top = 0
down = len(matrix) - 1
while True:
for i in range(left, right + 1):
arr.append(matrix[top][i])
top += 1
if top > down:
break
for i in range(top, down + 1):
arr.append(matrix[i][right])
right -= 1
if left > right:
break
for i in range(right, left - 1, -1):
arr.append(matrix[down][i])
down -= 1
if top > down:
break
for i in range(down, top - 1, -1):
arr.append(matrix[i][left])
left += 1
if left > right:
break
return arr
```
54 ,
Java Code:
```java
class Solution {
public int[][] generateMatrix(int n) {
@ -147,3 +184,45 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
import numpy as np
class Solution:
def generateMatrix(self, n: int)->List[List[int]]:
arr = np.array([[0] * n] * n)
left = 0
right = n - 1
top = 0
buttom = n - 1
num = 1
numsize = n * n
while True:
for i in range(left, right + 1):
arr[top][i] = num
num += 1
top += 1
if num > numsize:
break
for i in range(top, buttom + 1):
arr[i][right] = num
num += 1
right -= 1
if num > numsize:
break
for i in range(right, left - 1, -1):
arr[buttom][i] = num
num += 1
buttom -= 1
if num > numsize:
break
for i in range(buttom, top - 1, -1):
arr[i][left] = num
num += 1
left += 1
if num > numsize:
break
return arr.tolist()
```

View File

@ -44,6 +44,8 @@
10
Java Code:
```java
class Solution {
public int[] plusOne(int[] digits) {
@ -65,3 +67,21 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def plusOne(self, digits: List[int])->List[int]:
#
leng = len(digits)
for i in range(leng - 1, -1, -1):
digits[i] = (digits[i] + 1) % 10
# 0
if digits[i] != 0:
return digits
# 01
arr = [0] * (leng + 1)
arr[0] = 1
return arr
```

View File

@ -38,6 +38,8 @@
Java Code:
```java
class Solution {
public void sortColors(int[] nums) {
@ -65,6 +67,35 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def sortColors(self, nums: List[int]):
leng = len(nums)
left = 0
#
i = left
right = leng - 1
while i <= right:
if nums[i] == 2:
self.swap(nums, i, right)
right -= 1
elif nums[i] == 0:
self.swap(nums, i, left)
i += 1
left += 1
else:
i += 1
return nums
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
使
[0,0,0,1,1,1,2,2,2]
@ -83,6 +114,8 @@ class Solution {
Java Code:
```java
class Solution {
public void sortColors(int[] nums) {
@ -114,3 +147,31 @@ class Solution {
}
```
Python3 Code:
```python
from typing import List
class Solution:
def sortColors(self, nums: List[int]):
left = 0
leng = len(nums)
right = leng - 1
i = 0
while i <= right:
if nums[i] == 0:
self.swap(nums, i, left)
left += 1
if nums[i] == 2:
self.swap(nums, i, right)
right -= 1
# 1 i i--
if nums[i] != 1:
i -= 1
i += 1
return nums
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```

View File

@ -45,6 +45,22 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def findRepeatNumber(self, nums: List[int])->int:
s = set()
for x in nums:
#
if x in s:
return x
#
s.add(x)
return -1
```
#### ****
****
@ -101,3 +117,22 @@ public:
};
```
Python3 Code:
```python
from typing import List
class Solution:
def findRepeatNumber(self, nums: List[int])->int:
if len(nums) == 0:
return -1
for i in range(0, len(nums)):
while nums[i] != i:
#
if nums[i] == nums[nums[i]]:
return nums[i]
#
temp = nums[i]
nums[i] = nums[temp]
nums[temp] = temp
return -1
```

View File

@ -82,3 +82,27 @@ public:
};
```
Python3 Code:
```python
from typing import List
import sys
class Solution:
def minSubArrayLen(self, s: int, nums: List[int])->int:
leng = len(nums)
windowlen = sys.maxsize
i = 0
sum = 0
for j in range(0, leng):
sum += nums[j]
while sum >= s:
windowlen = min(windowlen, j - i + 1)
sum -= nums[i]
i += 1
if windowlen == sys.maxsize:
return 0
else:
return windowlen
```