Skip to content

Commit

Permalink
Ruby solution for Permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
saip7795 committed Jan 16, 2023
1 parent c5ec43f commit 16a8041
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ruby/0046-permutations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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

0 comments on commit 16a8041

Please sign in to comment.