Permutations
Given a collection of distinct integers, return all possible permutations.
Example 1
1 | Input: [1,2,3] |
解题思路
回溯法。如果排列中已经存在某元素,则跳过该元素。
复杂度分析
时间复杂度:列举所有情况,$O(n^n)$。
空间复杂度:调用栈最多为
n
层,$O(n)$。
代码
1 | class Solution { |
Blog of Chungen lchungen@qq.com
Given a collection of distinct integers, return all possible permutations.
1 | Input: [1,2,3] |
回溯法。如果排列中已经存在某元素,则跳过该元素。
时间复杂度:列举所有情况,$O(n^n)$。
空间复杂度:调用栈最多为 n
层,$O(n)$。
1 | class Solution { |