Leetcode 46 Permutations

Posted Fourth Dimension

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 46 Permutations相关的知识,希望对你有一定的参考价值。

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
# @param {Integer[]} nums
# @return {Integer[][]}
def permute(nums)
    return [[]] if nums.empty?
    a = Array.new(nums).map{|x| [x]}
    while a[0].length < nums.length
        b = Array.new()
        a.each do |x|
            (nums-x).each {|y| b << x + [y]}
        end
        a = b
    end
    a
end

 

以上是关于Leetcode 46 Permutations的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 46-Permutations and 47-Permutations II

[LeetCode] 46. Permutations(全排列)

[leetcode][46] Permutations

LeetCode 46. Permutations

LeetCode 46: Permutations

Leetcode 46 Permutations