Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lcof2/剑指 Offer II 101. 分割等和子串/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ func canPartition(nums []int) bool {
}
```

#### Swift

```swift
class Solution {
func canPartition(_ nums: [Int]) -> Bool {
let s = nums.reduce(0, +)
if s % 2 != 0 { return false }
let target = s / 2
var dp = Array(repeating: false, count: target + 1)
dp[0] = true

for num in nums {
for j in stride(from: target, through: num, by: -1) {
dp[j] = dp[j] || dp[j - num]
}
}

return dp[target]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 17 additions & 0 deletions lcof2/剑指 Offer II 101. 分割等和子串/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
func canPartition(_ nums: [Int]) -> Bool {
let s = nums.reduce(0, +)
if s % 2 != 0 { return false }
let target = s / 2
var dp = Array(repeating: false, count: target + 1)
dp[0] = true

for num in nums {
for j in stride(from: target, through: num, by: -1) {
dp[j] = dp[j] || dp[j - num]
}
}

return dp[target]
}
}
Loading