0046. Permutations (M)
Permutations (M)
题目
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]
]
题意
输出给定数组的全排列。
思路
- 排列组合常用回溯法:
- 使用hash:用一张hash表记录对应下标的数是否已被使用,每次操作都先往已有序列后插入一个未被使用的数,更新hash,递归,再移除改数,更新hash。
- 不使用hash:对于结果序列中的每一个位置,它可能存放的数为nums从当前位置到最后一个位置中的任意一个数(因为各异),所以每次操作都从这些数中选一个放到当前空位,再对右边的空格进行递归操作。
- 结合 31. Next Permutation 来实现全排列。
- 通过在长度为n的已有序列中的(n+1)个间隔位置插入数,重复操作来实现全排列。
代码实现
Java
回溯法hash
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
permute(nums, new boolean[nums.length], new ArrayList<>(), ans);
return ans;
}
// hash用来标记对应下标是否已经被使用过
private void permute(int[] nums, boolean[] hash, List<Integer> list, List<List<Integer>> ans) {
if (list.size() == nums.length) {
ans.add(new ArrayList<>(list));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!hash[i]) {
list.add(nums[i]);
hash[i] = true;
permute(nums, hash, list, ans);
list.remove(list.size() - 1);
hash[i] = false;
}
}
}
}
回溯法无hash
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
permute(nums, 0, ans);
return ans;
}
private void permute(int[] nums, int index, List<List<Integer>> ans) {
if (index == nums.length) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
ans.add(list);
return;
}
// 每次更新当前位置处的数字
for (int i = index; i < nums.length; i++) {
swap(nums, index, i);
permute(nums, index + 1, ans);
swap(nums, index, i);
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
nextPermutation
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
while (true) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
ans.add(list);
if (hasNextPermutation(nums)) {
nextPermutation(nums);
} else {
break;
}
}
return ans;
}
// 根据当前排列计算下一个排列
private void nextPermutation(int[] nums) {
int i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
int j = nums.length - 1;
while (nums[j] <= nums[i]) {
j--;
}
swap(nums, i, j);
reverse(nums, i + 1, nums.length - 1);
}
// 判断是否存在下一个排列,即判断是否已经是完全逆序数列
private boolean hasNextPermutation(int[] nums) {
int i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
return i >=0;
}
private void reverse(int[] nums, int left, int right) {
while (left < right) {
swap(nums, left++, right--);
}
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
插入法
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
// 第一重循环确定每次要插入的数的下标
for (int i = 0; i < nums.length; i++) {
int size = ans.size();
// 第二重循环控制更新所有待插入数列
for (int j = 0; j < size; j++) {
List<Integer> cur = ans.remove(0);
// 第三重循环选择在cur.size()+1个间隔位置处插入nums[i]
for (int k = 0; k <= cur.size(); k++) {
List<Integer> temp = new ArrayList<>(cur);
temp.add(k, nums[i]);
ans.add(temp);
}
}
}
return ans;
}
}
JavaScript
回溯法hash
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
let lists = []
dfs(nums, 0, [], lists, new Set())
return lists
}
let dfs = function (nums, index, list, lists, used) {
if (index === nums.length) {
lists.push([...list])
return
}
for (let i = 0; i < nums.length; i++) {
if (!used.has(i)) {
used.add(i)
list.push(nums[i])
dfs(nums, index + 1, list, lists, used)
list.pop()
used.delete(i)
}
}
}
回溯法无hash
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
let lists = []
dfs(nums, 0, lists)
return lists
}
let dfs = function (nums, index, lists) {
if (index === nums.length) {
lists.push([...nums])
return
}
for (let i = index; i < nums.length; i++) {
[nums[index], nums[i]] = [nums[i], nums[index]]
dfs(nums, index + 1, lists);
[nums[index], nums[i]] = [nums[i], nums[index]]
}
}
nextPermutation
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
let lists = []
nums.sort((a, b) => a - b)
lists.push([...nums])
while (nextPos(nums) >= 0) {
lists.push([...nextPermutation(nums)])
}
return lists
}
let nextPos = function (nums) {
let i = nums.length - 2
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--
}
return i
}
let nextPermutation = function (nums) {
let index = nextPos(nums)
for (let i = nums.length - 1; i > index; i--) {
if (nums[i] > nums[index]) {
[nums[i], nums[index]] = [nums[index], nums[i]]
break
}
}
let left = index + 1, right = nums.length - 1
while (left < right) {
[nums[left++], nums[right--]] = [nums[right], nums[left]]
}
return nums
}
插入法
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
let lists = [[]]
for (let i = 0; i < nums.length; i++) {
let size = lists.length
for (let j = 0; j < size; j++) {
let array = lists.shift()
for (let k = 0; k <= array.length; k++) {
let copy = [...array]
copy.splice(k, 0, nums[i])
lists.push(copy)
}
}
}
return lists
}