Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 780e670

Browse files
feat: add swift implementation to lcof2 problem: No.104 (doocs#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‎

Lines changed: 21 additions & 0 deletions
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 -->
Lines changed: 16 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /