代码如下:
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
l,i,r= 0,len(nums)-1,len(nums)-1
res = [float('inf')]*len(nums)
while l<=i:
if nums[l]**2<nums[i]**2:
res[r] = nums[i]**2
i-=1
else:
res[r] = nums[l]**2
l+=1
r-=1
return res
滑动窗口法
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
l=len(nums)
right = 0
left = 0
cur_sum=0
min_len = float('inf')
while right < l:
cur_sum+=nums[right]
while cur_sum>=target:
min_len = min(right-left+1,min_len)
cur_sum-=nums[left]
left+=1
right+=1
return min_len if min_len!=float('inf') else 0
error:
学会了if,else新用法
分不清左右顺序混乱
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
nums = [[0]*n for _ in range(n)]
start_x=0
start_y=0
count=1
loop,mid = n//2,n//2
for offset in range(1,loop+1):
for i in range(start_y,n-offset):
nums[start_x][i] = count
count+=1
for i in range(start_x,n-offset):
nums[i][n-offset] = count
count+=1
for i in range(n-offset,start_y,-1):
nums[n-offset][i] = count
count+=1
for i in range(n-offset,start_x,-1):
nums[i][start_y] = count
count+=1
start_x+=1
start_y+=1
if n%2!=0:
nums[mid][mid]=n**2
return nums
error
逻辑错误
坚持
坚持
坚持就是胜利!!!
个人:不要太在乎别人的看法,活出精彩。