My recursive Ruby solution


  • 0
    T
    def permute(nums)
        return [[]] if nums.empty?
        
        perms = []
        
        (0...nums.length).each do |i|
            el = nums[i]
            rest = nums.take(i) + nums.drop(i + 1)
            
            new_perms = permute(rest).map { |perm| perm.unshift(el) }
            perms.concat(new_perms)
        end
        
        perms
    end
    

Log in to reply
 

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