Advertisement
HXXXXJ

518. Coin Change 2

Feb 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.43 KB | None | 0 0
  1.     //f(x) = n
  2.     //拼出x块钱可以的方法数
  3.     func change(_ amount: Int, _ coins: [Int]) -> Int {
  4.         var arr = [Int](repeating: 0, count: amount + 1)
  5.         arr[0] = 1
  6.         for coin in coins{
  7.             for i in stride(from: 1, to: amount + 1, by:1){
  8.                 if i - coin >= 0{
  9.                     arr[i] += arr[i - coin]
  10.                 }
  11.             }
  12.         }
  13.         return arr[amount]
  14.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement