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) {
169169};
170170```
171171
172+ #### Swift
173+
174+ ``` swift
175+ class Solution {
176+ func coinChange (_ coins : [Int ], _ amount : Int ) -> Int {
177+
178+ var dp = [Int ](repeating : amount + 1 , count : amount + 1 )
179+ dp[0 ] = 0
180+
181+ for coin in coins {
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+
172192<!-- tabs: end -->
173193
174194<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func coinChange( _ coins: [ Int ] , _ amount: Int ) -> Int {
3+
4+ var dp = [ Int] ( repeating: amount + 1 , count: amount + 1 )
5+ dp [ 0 ] = 0
6+
7+ for coin in coins {
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