We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 08199d5 commit 0d2d732Copy full SHA for 0d2d732
problems/0416.分割等和子集.md
@@ -238,6 +238,32 @@ class Solution:
238
return taraget == dp[taraget]
239
```
240
Go:
241
+```go
242
+// 分割等和子集 动态规划
243
+// 时间复杂度O(n^2) 空间复杂度O(n)
244
+func canPartition(nums []int) bool {
245
+ sum := 0
246
+ for _, num := range nums {
247
+ sum += num
248
+ }
249
+ // 如果 nums 的总和为奇数则不可能平分成两个子集
250
+ if sum % 2 == 1 {
251
+ return false
252
253
+
254
+ target := sum / 2
255
+ dp := make([]int, target + 1)
256
257
258
+ for j := target; j >= num; j-- {
259
+ if dp[j] < dp[j - num] + num {
260
+ dp[j] = dp[j - num] + num
261
262
263
264
+ return dp[target] == target
265
+}
266
+```
267
268
269
javaScript:
0 commit comments