Skip to content

Commit 780e670

Browse files
authored
feat: add swift implementation to lcof2 problem: No.104 (#3617)
1 parent e4d9550 commit 780e670

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lcof2/剑指 Offer II 104. 排列的数目/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ func combinationSum4(nums []int, target int) int {
141141
}
142142
```
143143

144+
#### Swift
145+
146+
```swift
147+
class Solution {
148+
func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
149+
var dp = [Int](repeating: 0, count: target + 1)
150+
dp[0] = 1
151+
152+
for i in 1...target {
153+
for num in nums {
154+
if i >= num, dp[i] <= Int.max - dp[i - num] {
155+
dp[i] += dp[i - num]
156+
}
157+
}
158+
}
159+
160+
return dp[target]
161+
}
162+
}
163+
```
164+
144165
<!-- tabs:end -->
145166

146167
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func combinationSum4(_ nums: [Int], _ target: Int) -> Int {
3+
var dp = [Int](repeating: 0, count: target + 1)
4+
dp[0] = 1
5+
6+
for i in 1...target {
7+
for num in nums {
8+
if i >= num, dp[i] <= Int.max - dp[i - num] {
9+
dp[i] += dp[i - num]
10+
}
11+
}
12+
}
13+
14+
return dp[target]
15+
}
16+
}

0 commit comments

Comments
 (0)