代码随想录算法训练营第二天|209.长度最小的子数组

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        min_len=float('inf')
        i=0
        j=0
        sums=0
        l=len(nums)
        while j<l:
            sums+=nums[j]

            while sums>=target:
                min_len=min(j-i+1,min_len)
                sums-=nums[i]
                i+=1
            j+=1
        return min_len if min_len!=float('inf') else 0

如果将第二个while换成if将会出现什么情况?

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        nums=[[0]*n for _ in range(n)]
        startx,starty=0,0
        loop,mid=n//2,n//2
        count=1

        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 !=0:
            nums[mid][mid]=count
        return nums

全部评论

相关推荐

BOSS未读或已读不回,是要一直等还是再问打扰一下HR
程序员小白条:肯定是持久战,一直发会有效果的,总比腼腆不发好
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务