赞
踩
【简单题】【题目链接】
## 思路一中的找V字形中abs最小值位置,后续实现略 def findminabs(nums): if not nums: return None left,right = 0,len(nums)-1 if nums[left]>=0: mid = 0 else: while(left<=right): mid = left + (right-left)//2 if nums[left]*nums[mid]>0: left = mid+1 elif nums[right]*nums[mid]>0: right = mid-1 elif nums[mid]==0: break return mid
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
left,right = 0,len(nums)-1
ans = [0]*(right+1)
index = right
while(left<=right):
if abs(nums[left])>=abs(nums[right]):
ans[index] = nums[left]**2
left += 1
elif abs(nums[left])<abs(nums[right]):
ans[index] = nums[right]**2
right -= 1
index-=1
return ans
## 第一次的写法 class Solution: def minSubArrayLen(self, target: int, nums: List[int]) -> int: SUM = 0 N = len(nums) left,right = 0,0 ans = float('inf') while(right<N): #while(SUM<target and right<N): ##这里的while可以进行精简 #SUM += nums[right] #right += 1 SUM += nums[right] right += 1 while(SUM>=target): ans = min(ans,right-left) SUM -= nums[left] left += 1 return 0 if ans==float('inf') else ans
【中等题】【题目链接】
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。