1-3 lines Ruby solutions


  • 2

    Solution 1

    def subsets(nums)
      0.upto(nums.sort!.size).flat_map{ |k| nums.combination(k).to_a }
    end
    

    Solution 2

    def subsets(nums)
      subsets = [[]]
      nums.sort.each { |n| subsets += subsets.map { |s| s + [n] } }
      subsets
    end
    

    Solution 3

    def subsets(nums)
      nums.sort.reduce([[]]) { |subsets, n| subsets + subsets.map { |s| s + [n] } }
    end

Log in to reply
 

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