[LeetCode] 216. Combination Sum III(Kotlin)
풀이
class Solution {
private val answer: MutableList<List<Int>> = mutableListOf()
private var _k = -1
private var _n = -1
fun combinationSum3(k: Int, n: Int): List<List<Int>> {
_k = k
_n = n
getCombination(1, listOf())
return answer.toList()
}
private fun getCombination(curNum:Int, combination:List<Int>){
val comSum = combination.sum()
val comSize = combination.size
if((comSum > _n)||(comSize > _k)) return
if(comSize == _k){
if(comSum == _n) answer.add(combination)
return
}
if(curNum >= 10) return
getCombination(curNum+1, combination)
getCombination(curNum+1, combination + listOf(curNum))
}
}