Skip to content

Commit 250a32b

Browse files
committed
update: some array topics
1 parent 43e9958 commit 250a32b

5 files changed

+178
-0
lines changed

arr.container-with-most-water.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
盛最多水的容器
4+
https://leetcode-cn.com/problems/container-with-most-water/
5+
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。
6+
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
7+
说明:你不能倾斜容器,且 n 的值至少为 2。
8+
"""
9+
def maxArea(self, height):
10+
maxArea = 0
11+
i = 0
12+
j = len(height) - 1
13+
while(i < j):
14+
minHeight = min(height[i], height[j])
15+
maxArea = max((j - i) * minHeight, maxArea)
16+
if height[i] < height[j]:
17+
i += 1
18+
else:
19+
j -= 1
20+
21+
return maxArea
22+
23+
so = Solution()
24+
print(so.maxArea([1,8,6,2,5,4,8,3,7]))
25+

arr.move-zeroes.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
283. 移动零
6+
https://leetcode-cn.com/problems/move-zeroes/
7+
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
8+
Do not return anything, modify nums in-place instead.
9+
"""
10+
def moveZeroes(self, nums: List[int]) -> None:
11+
idx = 0
12+
for i in range(len(nums)):
13+
if nums[i] != 0:
14+
nums[idx], nums[i] = nums[i], nums[idx]
15+
idx += 1
16+
17+
18+
so = Solution()
19+
nums = [0, 1, 0, 3, 12]
20+
so.moveZeroes(nums)
21+
print(nums)
22+

arr.three-sum.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
15. 三数之和
6+
https://leetcode-cn.com/problems/3sum/
7+
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
8+
注意:答案中不可以包含重复的三元组。
9+
"""
10+
# 双指针
11+
def threeSum(self, nums: List[int]) -> List[List[int]]:
12+
# 若数组长度小于3,则返回空数组
13+
res = []
14+
n = len(nums)
15+
if n < 3:
16+
return res
17+
18+
# 对数组进行排序
19+
nums.sort()
20+
for i in range(n-2):
21+
# 若 i 大于 0 则终止循环,返回结果
22+
if nums[i] > 0:
23+
return res
24+
25+
26+
# 对于重复元素,直接跳过
27+
if i > 0 and nums[i] == nums[i-1]:
28+
continue
29+
30+
# 声明左指针 L=i+1, 右指针 R=n-1,
31+
l = i + 1
32+
r = n - 1
33+
34+
while(l < r):
35+
num = nums[i] + nums[l] + nums[r]
36+
# 若 nums[i] + nums[L] + nums[R] = 0
37+
if num == 0:
38+
res.append([nums[i], nums[l], nums[r]])
39+
l += 1
40+
while nums[l] == nums[l-1] and l < r:
41+
l += 1
42+
r -= 1
43+
while nums[r] == nums[r+1] and l < r:
44+
r -= 1
45+
# 若和小于0,则说明 nums[L] 小了,L 向右移
46+
elif num < 0:
47+
l += 1
48+
# 若和大于0,则说明 nums[R] 打了,R 向左移
49+
else:
50+
r -= 1
51+
return res
52+
53+
# 暴力破解
54+
def threeSumByForce(self, nums: List[int]) -> List[List[int]]:
55+
res = []
56+
nums.sort()
57+
for i in range(len(nums) - 2):
58+
if i > 0 and nums[i] == nums[i - 1]:
59+
continue
60+
for j in range(i + 1, len(nums) - 1):
61+
if j > i + 1 and nums[j] == nums[j - 1]:
62+
continue
63+
for m in range(j + 1, len(nums)):
64+
if m > j + 1 and nums[m] == nums[m - 1]:
65+
continue
66+
if nums[i] + nums[j] + nums[m] == 0:
67+
res.append([nums[i], nums[j], nums[m]])
68+
return res
69+
70+
71+
so = Solution()
72+
print(so.threeSum([-1,0,1,2,-1,-4]))
73+
print(so.threeSum([0, 0, 0, 0]))
74+
print(so.threeSum([-2,0,0,2,2]))
75+

arr.two-sum.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
1. 两数之和
7+
https://leetcode-cn.com/problems/two-sum/
8+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
9+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
10+
"""
11+
# hash map
12+
def twoSum(self, nums: List[int], target: int) -> List[int]:
13+
map = {}
14+
for i, num in enumerate(nums):
15+
if map.get(target - num) is not None:
16+
return [map.get(target - num), i]
17+
map[num] = i
18+
19+
return []
20+
21+
# 暴力破解
22+
def twoSum1(self, nums: List[int], target: int) -> List[int]:
23+
res = []
24+
for i in range(len(nums) - 1):
25+
for j in range(i + 1, len(nums)):
26+
if (nums[i] + nums[j]) == target :
27+
res = [i, j]
28+
break
29+
30+
return res
31+
32+
33+
so = Solution()
34+
print(so.twoSum(nums=[2, 7, 11, 15], target=9))
35+

dy.climbing-stairs.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
class Solution:
3+
"""
4+
70.爬楼梯
5+
https://leetcode-cn.com/problems/climbing-stairs/
6+
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
7+
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
8+
注意:给定 n 是一个正整数。
9+
"""
10+
def climbStairs(self, n: int) -> int:
11+
dp = {}
12+
dp[1] = 1
13+
dp[2] = 2
14+
for i in range(3, n+1):
15+
dp[i] = dp[i-1] + dp[i-2]
16+
17+
return dp[n]
18+
19+
so = Solution()
20+
print(so.climbStairs(4))
21+

0 commit comments

Comments
 (0)