mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Update 0416.分割等和子集.md
This commit is contained in:
@ -296,62 +296,85 @@ false true false false false true true false false false true true
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python:
|
### Python:
|
||||||
|
卡哥版
|
||||||
```python
|
```python
|
||||||
# 一维度数组解法
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def canPartition(self, nums: List[int]) -> bool:
|
def canPartition(self, nums: List[int]) -> bool:
|
||||||
target = sum(nums)
|
_sum = 0
|
||||||
if target % 2 == 1: return False
|
|
||||||
target //= 2
|
# dp[i]中的i表示背包内总和
|
||||||
dp = [0] * (target + 1)
|
# 题目中说:每个数组中的元素不会超过 100,数组的大小不会超过 200
|
||||||
for i in range(len(nums)):
|
# 总和不会大于20000,背包最大只需要其中一半,所以10001大小就可以了
|
||||||
for j in range(target, nums[i] - 1, -1):
|
dp = [0] * 10001
|
||||||
dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
|
for num in nums:
|
||||||
return target == dp[target]
|
_sum += num
|
||||||
|
# 也可以使用内置函数一步求和
|
||||||
|
# _sum = sum(nums)
|
||||||
|
if _sum % 2 == 1:
|
||||||
|
return False
|
||||||
|
target = _sum // 2
|
||||||
|
|
||||||
|
# 开始 0-1背包
|
||||||
|
for num in nums:
|
||||||
|
for j in range(target, num - 1, -1): # 每一个元素一定是不可重复放入,所以从大到小遍历
|
||||||
|
dp[j] = max(dp[j], dp[j - num] + num)
|
||||||
|
|
||||||
|
# 集合中的元素正好可以凑成总和target
|
||||||
|
if dp[target] == target:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
```
|
```
|
||||||
|
二维DP版
|
||||||
```python
|
```python
|
||||||
# 二维度数组解法
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def canPartition(self, nums: List[int]) -> bool:
|
def canPartition(self, nums: List[int]) -> bool:
|
||||||
target = sum(nums)
|
|
||||||
nums = sorted(nums)
|
total_sum = sum(nums)
|
||||||
|
|
||||||
# 做最初的判断
|
if total_sum % 2 != 0:
|
||||||
if target % 2 != 0:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 找到 target value 可以认为这个是背包的体积
|
target_sum = total_sum // 2
|
||||||
target = target // 2
|
dp = [[False] * (target_sum + 1) for _ in range(len(nums) + 1)]
|
||||||
|
|
||||||
row = len(nums)
|
# 初始化第一行(空子集可以得到和为0)
|
||||||
col = target + 1
|
for i in range(len(nums) + 1):
|
||||||
|
dp[i][0] = True
|
||||||
|
|
||||||
# 定义 dp table
|
for i in range(1, len(nums) + 1):
|
||||||
dp = [[0 for _ in range(col)] for _ in range(row)]
|
for j in range(1, target_sum + 1):
|
||||||
|
if j < nums[i - 1]:
|
||||||
# 初始 dp value
|
# 当前数字大于目标和时,无法使用该数字
|
||||||
for i in range(row):
|
|
||||||
dp[i][0] = 0
|
|
||||||
|
|
||||||
for j in range(1, target):
|
|
||||||
if nums[0] <= j:
|
|
||||||
dp[0][j] = nums[0]
|
|
||||||
|
|
||||||
# 遍历 先遍历物品再遍历背包
|
|
||||||
for i in range(1, row):
|
|
||||||
|
|
||||||
cur_weight = nums[i]
|
|
||||||
cur_value = nums[i]
|
|
||||||
|
|
||||||
for j in range(1, col):
|
|
||||||
if cur_weight > j:
|
|
||||||
dp[i][j] = dp[i - 1][j]
|
dp[i][j] = dp[i - 1][j]
|
||||||
else:
|
else:
|
||||||
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight] + cur_value)
|
# 当前数字小于等于目标和时,可以选择使用或不使用该数字
|
||||||
|
dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]]
|
||||||
# 输出结果
|
|
||||||
return dp[-1][col - 1] == target
|
return dp[len(nums)][target_sum]
|
||||||
|
|
||||||
|
```
|
||||||
|
一维DP版
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def canPartition(self, nums: List[int]) -> bool:
|
||||||
|
|
||||||
|
total_sum = sum(nums)
|
||||||
|
|
||||||
|
if total_sum % 2 != 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
target_sum = total_sum // 2
|
||||||
|
dp = [False] * (target_sum + 1)
|
||||||
|
dp[0] = True
|
||||||
|
|
||||||
|
for num in nums:
|
||||||
|
# 从target_sum逆序迭代到num,步长为-1
|
||||||
|
for i in range(target_sum, num - 1, -1):
|
||||||
|
dp[i] = dp[i] or dp[i - num]
|
||||||
|
|
||||||
|
return dp[target_sum]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
Reference in New Issue
Block a user