---
# System prepended metadata

title: 0046. Permutations
tags: [Leetcode, FaceBook, Backtracking]

---

# 0046. Permutations
###### tags: `Leetcode` `FaceBook` `Backtracking`
Link: https://leetcode.com/problems/permutations/
## 思路
这种需要固定长度的permutation都是用交换做的 类似的题目还有 [267. Palindrome Permutation II](https://hackmd.io/skACPwENSTK5eoMhHuVJfA)
![](https://i.imgur.com/MU5icLd.png)
## Code
```java=
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> output = new ArrayList<>();
        int n = nums.length;
        List<Integer> numsList = new ArrayList<>();
        for(int i = 0;i < nums.length;i++){
            numsList.add(nums[i]);
        }
        backtrack(n, numsList, output,0);
        return output;
    }
    public void backtrack(int len, List<Integer> nums, List<List<Integer>> output, int first){
        if(first == len){
            output.add(new ArrayList<Integer>(nums));
        }
        for(int i = first;i < len;i++){
            Collections.swap(nums, first, i);
            backtrack(len, nums, output, first+1);
            Collections.swap(nums, first, i);
        }
    }
}
```
