2019年8月28日——leetcode

716.特殊的二进制序列

https://leetcode-cn.com/problems/special-binary-string/
太难不解,无意义


309. 最佳买卖股票时机含冷冻期

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
股票类,另出专题


416.分割等和子集

https://leetcode-cn.com/problems/partition-equal-subset-sum/

class Solution:
    def canPartition(self, nums: List[int]) -> bool:
        size = len(nums)

        # 特判,如果整个数组的和都不是偶数,就无法平分
        s = sum(nums)
        if s & 1 == 1:
            return False

        # 二维 dp 问题:背包的容量
        target = s // 2
        dp = [[False for _ in range(target + 1)] for _ in range(size)]

        # 先写第 1 行:看看第 1 个数是不是能够刚好填满容量为 target
        for i in range(target + 1):
            dp[0][i] = False if nums[0] != i else True
        # i 表示物品索引
        for i in range(1, size):
            # j 表示容量
            for j in range(target + 1):
                if j >= nums[i]:
                    dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i]]
                else:
                    dp[i][j] = dp[i - 1][j]

        return dp[-1][-1]

2.两数相加

https://leetcode-cn.com/problems/add-two-numbers/solution/

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        result = ListNode(0)
        cur = result
        carry = 0
        while l1 or l2:
            x = l1.val if l1 else 0
            y = l2.val if l2 else 0
            the_sum = x + y + carry
            carry = the_sum // 10
            cur.next = ListNode(the_sum % 10)
            cur = cur.next
            if l1: l1 = l1.next 
            if l2: l2 = l2.next 
        if carry > 0:
            cur.next = ListNode(carry)
        return result.next
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值