977 有序数组的平方
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
i=0
j=len(nums)-1
k=len(nums)-1
A=[float('inf')]*len(nums)
while (i<=j):
if nums[i]*nums[i]>=nums[j]*nums[j]:
A[k]=nums[i]*nums[i]
i+=1
k-=1
else:
A[k]=nums[j]*nums[j]
j-=1
k-=1
return A
209 长度最小的有序数组
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
sum=0
sumL=0
result=float('inf')
i=0
for j in range(len(nums)):
sum+=nums[j]
while sum >= target:
sumL=j-i+1
result=min(result,sumL)
sum-=nums[i]
i+=1
if result !=float('inf'):
return result
else:
return 0
59 螺旋矩阵 2
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
nums=[[0]*n for _ in range(n)]
startx=0
starty=0
offset=1
count=1
loop=n//2
mid=n//2
for offset in range(1,loop+1):
for i in range(starty,n-offset):
nums[startx][i]=count
count+=1
for i in range(startx,n-offset):
nums[i][n-offset]=count
count+=1
for i in range(n-offset,starty,-1):
nums[n-offset][i]=count
count+=1
for i in range(n-offset,startx,-1):
nums[i][starty]=count
count+=1
startx+=1
starty+=1
if n/2!= n//2:
nums[mid][mid]+=count
return nums