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 1b60ca6 commit 1b07cb7Copy full SHA for 1b07cb7
Dynamic Programming/416. 分割等和子集.md
@@ -97,3 +97,33 @@ func canPartition(nums []int) bool {
97
}
98
```
99
100
+
101
102
+一维数组:
103
104
+`dp[i]` 表示是否存在和为 `i` 的 num 组合。`target` 为数组和的二分之一,因为是求两个等和子集。
105
106
+- 外层遍历 nums 每个 num;
107
+- 内层遍历 target(由大到小)。
108
109
+```go
110
+func canPartition(nums []int) bool {
111
+ sum := 0
112
+ for _, num := range nums {
113
+ sum += num
114
+ }
115
+ if sum % 2 == 1 {
116
+ return false
117
118
+ target := sum / 2
119
+ dp := make([]bool, target + 1)
120
+ dp[0] = true
121
122
+ for j := target; j >= num; j-- {
123
+ dp[j] = dp[j] || dp[j - num]
124
125
126
+ return dp[target]
127
+}
128
+```
129
0 commit comments