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