> For the complete documentation index, see [llms.txt](https://wentao-shao.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wentao-shao.gitbook.io/leetcode/permutation-and-combination/46.permutations.md).

# 46.Permutations

## 46. Permutations

## 题目地址

<https://www.lintcode.com/en/problem/permutations/>

<https://leetcode.com/problems/permutations/>

## 题目描述

```
Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
```

## 代码

### Approach #1 Recursion

```java
public class Solution {
    public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    if (nums == null || nums.length == 0) return result; 

     List<Integer> list = new ArrayList<Integer>();
     dfs(nums, list, result);

    return result;
  }

  private void dfs(int[] nums, List<Integer> list, List<List<Integer>> result) {
    if (list.size() == nums.length) {
      result.add(new ArrayList<Integer>(list));
      return;
    }

    for (int i = 0; i < nums.length; i++) {
      if (list.contains(nums[i])) continue;
      list.add(nums[i]);
      dfs(nums, list, result);
      list.remove(list.size() - 1);
    }
  }

}
```

### Approach #2 Recursion

```java
public class Solution {
  public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    List<Integer> numsList = new ArrayList<Integer>();

    if (nmus == null) {
      return result;
    } else {
      // convert int[] to List<Integer>
      for (int item : nums) numsList.add(item);
    }

    if (nums.length <= 1) {
      result.add(numsList);
      return result;
    }

    for (int i = 0; i < nums.length; i++) {
      int[] numsNew = new int[nums.length - 1];
      System.arraycopy(nums, 0, numsNew, 0, i);
      System.arraycopy(nums, i + 1, numsNew, i, nums.length - i - 1);
    }

  }
}
```

Approach Backtrack

```java
public class Solution {
  public void backtrack(int n, ArrayList<Integer> nums,
                       List<List<Integer>> output,
                       int first) {
    if (first == n)
      output.add(new ArrayList<Integer>(nums));
    for (int i = first; i < n; i++) {
      Collections.swap(nums, first, i);
      backtack(n, nums, output, first + 1);
      Collections.swap(nums, first, i);
    }
  }

  public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> output = new LinkedList();

    ArrayList<Integer> nums_1st = new ArrayList<Integer>();
    for (int num: nums)
      nums_1st.add(num);

    int n = nums.length;
    backtrack(n, nums_1st, output, 0);
    return output;
  }
}
```

Approach 3: 字典序

```java
public class Solution {
  public List<List<Integer>> permute(int[] nums) {
    List<List<Integer>> result = new ArrayList<List<Integer>();
    if (nums == null || nums.length == 0) return result;

    int[] perm = Arrays.copyOf(nums, nums.length);
    // sort first
    Arrays.sort(perm);

    while (true) {
      List<Integer> tempList = new ArrayList<Integer>();
      for (int i: perm) tempList.add(i);
      result.add(tempList);

      // step1: search the last perm[k] < perm[k+1] 
      int k = -1;
      for (int i = perm.length - 2; i >= 0; i--) {
        if (perm[i] < perm[i + 1]) {
          k = i;
          break;
        }
      }
      // if current rank is the largest, exit while loop
      if (k == -1) break;

      // step2: search the first perm[k] < perm[l]
        int l = perm.length - 1;
      while (l > k && perm[l] <= perm[k]) l--;

      // step3: swap perm[k] with perm[l]
      int temp = perm[k];
      perm[k] = perm[l];
      perm[l] = temp;

      // step4: reverse between k+1 and perm.length-1
      reverse(perm, k+1, perm.length - 1)
    }

        return result;
  }

  private void reverse(int[] nums, int left, int right) {
    for (int l = left, r = right; l < r; l++, r--) {
      int temp = nums[l];
      nums[l] = nums[r];
      nums[r] = temp;
    }
  }

}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/permutation-and-combination/46.permutations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
