Advertisement
HXXXXJ

216. Combination Sum III

Jan 27th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.63 KB | None | 0 0
  1.     func combinationSum3(_ k: Int, _ n: Int) -> [[Int]] {
  2.         var res = [[Int]]()
  3.         var list = [Int]()
  4.         combination(k, 1, n, &list, &res)
  5.         return res
  6.     }
  7.    
  8.     func combination(_ count:Int, _ start:Int, _ remain: Int, _ list : inout [Int], _ res:inout [[Int]]){
  9.         if list.count == count {
  10.             if remain == 0 {
  11.                 res.append(list)
  12.             }
  13.             return
  14.         }
  15.         for i in stride(from: start, to: 10, by: 1){
  16.             list.append(i)
  17.             combination(count, i + 1, remain - i, &list, &res)
  18.             list.removeLast()
  19.         }
  20.        
  21.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement