代码随想录day31 ● 455.分发饼干 ● 376. 摆动序列 ● 53. 最大子序和

class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        g.sort()
        s.sort()
        res = 0
        j=0
        if not s:
            return 0
        i=0
        while(i<len(g)):
            if g[i] <= s[j] :
                res+=1
                j+=1
                i+=1
            else:
                j+=1
            if j== len(s):
                break
        return res

排序+双指针

class Solution(object):
    def wiggleMaxLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n < 2:
            return n
        preDiff = nums[1] - nums[0]
        ret = 1 if preDiff == 0  else 2
        for i in range(2,n):
            diff = nums[i] - nums[i-1]
            if (preDiff <= 0 and diff > 0) or (diff < 0 and preDiff >= 0):
                ret += 1
                preDiff = diff
        return ret

这题虽然有思路,但是自己写起来逻辑很乱,看答案就理顺了,当preDiff和diff异号时再更新preDiff,正好达到了删除元素的效果

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        cur = 0
        res = -99999999999
        for i in range(n):
            cur += nums[i]
            res = max(res,cur)
            cur = max(cur,0)
        return res

这题cur用来更新子数组首,res用来更新最大和

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值