functional style using ruby


  • 0
    U
    def combination_sum3(k, n)
        def comb_sum(k, n, start)
            return (n == 0? [[]] : []) unless k > 0
            return [] unless start <= 9
            comb_sum(k-1, n-start, start+1).map{|x|[start]+x} + comb_sum(k, n, start+1)
        end
        comb_sum(k, n, 1)
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.