Skip to content

Commit 8d49ea0

Browse files
authored
Create 416.swift
1 parent a63590a commit 8d49ea0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

400-500/416.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func canPartition(_ nums: [Int]) -> Bool {
3+
let arraySum = nums.reduce(0, +)
4+
if arraySum % 2 != 0 { return false }
5+
let target = arraySum / 2
6+
var dp = Array(repeating: false, count: target + 1)
7+
dp[0] = true
8+
9+
for num in nums {
10+
for i in stride(from: target, through: num, by: -1) {
11+
dp[i] = dp[i] || dp[i - num]
12+
if dp[target] { break }
13+
}
14+
}
15+
return dp[target]
16+
}
17+
}

0 commit comments

Comments
 (0)