File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
lcof2/剑指 Offer II 103. 最少的硬币数目 Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -169,6 +169,26 @@ var coinChange = function (coins, amount) {
169
169
};
170
170
```
171
171
172
+ #### Swift
173
+
174
+ ``` swift
175
+ class Solution {
176
+ func coinChange (_ coins : [Int ], _ amount : Int ) -> Int {
177
+ var dp = [Int ](repeating : amount + 1 , count : amount + 1 )
178
+ dp[0 ] = 0
179
+
180
+ for coin in coins {
181
+ if coin > amount { continue }
182
+ for j in coin... amount {
183
+ dp[j] = min (dp[j], dp[j - coin] + 1 )
184
+ }
185
+ }
186
+
187
+ return dp[amount] > amount ? -1 : dp[amount]
188
+ }
189
+ }
190
+ ```
191
+
172
192
<!-- tabs: end -->
173
193
174
194
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func coinChange( _ coins: [ Int ] , _ amount: Int ) -> Int {
3
+ var dp = [ Int] ( repeating: amount + 1 , count: amount + 1 )
4
+ dp [ 0 ] = 0
5
+
6
+ for coin in coins {
7
+ if coin > amount { continue }
8
+ for j in coin... amount {
9
+ dp [ j] = min ( dp [ j] , dp [ j - coin] + 1 )
10
+ }
11
+ }
12
+
13
+ return dp [ amount] > amount ? - 1 : dp [ amount]
14
+ }
15
+ }
You can’t perform that action at this time.
0 commit comments