Ruby Solution with Set


  • 0
    R
    require 'set'
    def permute_unique(nums)
        s = Set.new
        permute_unique_helper([], nums, s)
        s.to_a
    end
    
    def permute_unique_helper(prefix, nums, s)
        if nums.empty?
            s.add(prefix)
        else
            nums.each_with_index do |num, index|
                permute_unique_helper(prefix + [num], nums[0...index] + nums[index + 1...nums.length], s)
            end
        end
    end
    

Log in to reply
 

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