提交 d94ff936 编写于 作者: jhaos's avatar jhaos

Add new file

上级 8fab45a9
给定一个**只包含正整数**的非空数组。是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。
**注意:**
1. 每个数组中的元素不会超过 100
2. 数组的大小不会超过 200
**示例 1:**
```
输入: [1, 5, 11, 5]
输出: true
解释: 数组可以分割成 [1, 5, 5] 和 [11].
```
**示例 2:**
```
输入: [1, 2, 3, 5]
输出: false
解释: 数组不能分割成两个元素和相等的子集.
```
通过次数`65,635` | 提交次数`134,170`
**代码实现**
```python
class Solution:
def canPartition(self, nums: List[int]) -> bool:
if sum(nums)%2==1:
return False
target = sum(nums)>>1
dp = [False]*(target+1)
dp[0] = True
for i in range(len(nums)):
for j in range(target, nums[i]-1, -1):
dp[j] = dp[j] or dp[j-nums[i]]
return dp[-1]
```
```
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/partition-equal-subset-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册