Skip to content

Commit 2945b81

Browse files
authored
Create qqchangE_problem.md
1 parent 18a08ec commit 2945b81

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/qqchangE_problem.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
```python
2+
def canPartition(nums):
3+
def dfs(nums, target, num):
4+
for i in range(n):
5+
B = nums[:i] + nums[i + 1:]
6+
if num + nums[i] == target:
7+
return True
8+
elif num + nums[i] < target:
9+
if dfs(B, target, num + nums[i]):
10+
return True
11+
elif num == 0: # 有一个数比sum/2还大,直接返回False
12+
return False
13+
return False
14+
15+
n, total = len(nums), sum(nums)
16+
if total % 2 != 0: return False
17+
target = total // 2
18+
nums.sort(reverse=True) # 逆序排序,先从大的开始判断,速度会更快
19+
res = dfs(nums, target, 0)
20+
return res
21+
22+
23+
print(canPartition([3, 3, 3, 4, 5]))
24+
```

0 commit comments

Comments
 (0)