学习目标:
- 977.有序数组的平方
- 排序相关
- 209.长度最小的子数组
- 59.螺旋矩阵II
- 数组总结
学习内容:
977.有序数组的平方
977. 有序数组的平方 - 力扣(LeetCode)
https://leetcode.cn/problems/squares-of-a-sorted-array/
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
n = len(nums)
left = 0
right = n-1
ans = [0] *n
while left<=right:
if nums[left]+nums[right]<0:
ans[n-1] = nums[left]*nums[left]
left+=1
else:
ans[n-1] = nums[right]*nums[right]
right-=1
n-=1
return ans
排序(先放着 找个时间再总结一下)
(1条消息) 排序算法详解(堆,归并,快速排序最简及理解写法)_芒果冰mangoicel的博客-CSDN博客
https://blog.csdn.net/m0_60413302/article/details/127604251 十大排序算法超全大综合,动图演示,你真的值得拥有! - 力扣(LeetCode)
https://leetcode.cn/circle/article/rzsN73/
十大排序从入门到入赘 | yukiyama (iyukiyama.github.io)
https://iyukiyama.github.io/sorting/
209.长度最小的子数组
209. 长度最小的子数组 - 力扣(LeetCode)
https://leetcode.cn/problems/minimum-size-subarray-sum/
贴一篇关于滑动窗口求最大最小的题解 其中的模板可以思考一下
import math
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
ans = math.inf
n = len(nums)
left = 0
right = 0
cnt = 0
while right<n:
cnt+=nums[right]
right+=1
if cnt>=target:
while cnt>=target:
cnt-=nums[left]
left+=1
if right-left+1<ans:
ans = right-left+1
if ans ==1:
break
if ans == math.inf:
ans = 0
return ans
76. 最小覆盖子串 - 力扣(LeetCode)
https://leetcode.cn/problems/minimum-window-substring/
import math
class Solution:
def minWindow(self, s: str, t: str) -> str:
m = len(s)
n = len(t)
left = 0
right = 0
tmp = math.inf
ans = ""
dic = {}
kind = 0
for strs in t:
if strs not in dic:
dic[strs]=1
kind +=1
else:
dic[strs]+=1
for right in range(m):
if s[right] in dic:
dic[s[right]]-=1
if dic[s[right]] == 0:
kind-=1
if kind == 0:
while left<=right:
if s[left] in dic:
if dic[s[left]]==0:
if right-left+1<tmp:
tmp = right-left+1
ans = s[left:right+1]
break
dic[s[left]]+=1
left+=1
return ans
59.螺旋矩阵II
59. 螺旋矩阵 II - 力扣(LeetCode)
https://leetcode.cn/problems/spiral-matrix-ii/ 模拟 明确每圈逻辑 怎么转 别被转晕了 注意奇数情况中心点
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
result = [[0] *n for _ in range(n)]
#头闭尾开
row = 0 # 行
col = 0 # 列
k = 1
while k<=n*n:
if col==n-col-1:
result[col][col]=k
k+=1
# 左到右
for j in range(col,n-col-1):
result[row][j] = k
k+=1
# 上到下
for i in range(row,n-row-1):
result[i][n-col-1] = k
k+=1
# 右到左
for j in range(n-col-1,col,-1):
result[n-row-1][j] = k
k+=1
# 下到上
for i in range(n-row-1,row,-1):
result[i][col] = k
k+=1
row+=1
col+=1
return result