LeetCode 46 Permutations

原创 2016年06月01日 15:44:54

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], and [3,2,1].

方法一:逐渐生成前i个数的排列组合,然后把num[i+1]见缝插针,再组装成新的排列组合。见代码和打印结果。

	public static List<List<Integer>> permute(int[] nums) {
		List<List<Integer>> result = new ArrayList<>();
		result.add(new ArrayList<Integer>());
		for (int i = 0; i < nums.length; i++) {
			List<List<Integer>> newRes = new ArrayList<>();
			for (int j = 0; j <= i; j++)
				for (List<Integer> k : result) {
					List<Integer> list = new ArrayList<>(k);
					list.add(j, nums[i]);
					newRes.add(list);
				}
			result = newRes;
			System.out.println("tmp result = "+result);
		}
		return result;
	}
根据代码中的
System.out.println("tmp result = "+result);
nums = [0, 1, 2, 3]的打印结果:

tmp result = [[0]]
tmp result = [[1, 0], [0, 1]]
tmp result = [[2, 1, 0], [2, 0, 1], [1, 2, 0], [0, 2, 1], [1, 0, 2], [0, 1, 2]]
tmp result = [[3, 2, 1, 0], [3, 2, 0, 1], [3, 1, 2, 0], [3, 0, 2, 1], [3, 1, 0, 2], [3, 0, 1, 2], [2, 3, 1, 0], [2, 3, 0, 1], [1, 3, 2, 0], [0, 3, 2, 1], [1, 3, 0, 2], [0, 3, 1, 2], [2, 1, 3, 0], [2, 0, 3, 1], [1, 2, 3, 0], [0, 2, 3, 1], [1, 0, 3, 2], [0, 1, 3, 2], [2, 1, 0, 3], [2, 0, 1, 3], [1, 2, 0, 3], [0, 2, 1, 3], [1, 0, 2, 3], [0, 1, 2, 3]]

方法二:递归回溯.

It could be solved using modified DFS.  Each time insert one element that has not been inserted yet.

The idea it so start with empty set. Each time , one number is introduced, for each existing solution, insert this number to all possible positions. 

	public static List<List<Integer>> permute2(int[] nums) {
		List<List<Integer>> result = new ArrayList<>();
		backtrack(result, new ArrayList<Integer>(), nums);
		return result;
	}

	private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
		if (tempList.size() == nums.length) {
			result.add(new ArrayList<>(tempList));
		} else {
			for (int i = 0; i < nums.length; i++) {
				if (tempList.contains(nums[i])) continue; // element already exists, skip
				tempList.add(nums[i]);
				backtrack(result, tempList, nums);
				tempList.remove(tempList.size() - 1);
			}
		}
	}
方法三:对数组数据不断进行交换,这个方式最快

	public List<List<Integer>> permute3(int[] nums) {
		List<List<Integer>> result = new ArrayList<>();
		perm(result, nums, 0);
		return result;
	}

	public void perm(List<List<Integer>> result, int[] nums, int pos) {
		if(pos == nums.length) {
			List<Integer> list = new ArrayList<Integer>();
			for(int a:nums) list.add(a);
			result.add(list);
			return;
		}
		for(int i=pos; i<nums.length; i++) {
			swap(nums, i, pos);
			perm(result, nums, pos+1);
			swap(nums, i, pos);
		}
	}

	private void swap(int[] nums, int i, int j) {
		int tmp = nums[i];
		nums[i] = nums[j];
		nums[j] = tmp;
	}






相关文章推荐

LeetCode 46: Permutations

即求n个不重复的数的组合。 思路: 先固定第一位,枚举第二位,。。。第n位,注意需要记录已经用过的数字。用代码说话:public class Solution { public List ...

LeetCode 46. Permutations 解题报告

LeetCode 46. Permutations 解题报告

leetcode || 46、Permutations

problem: Given a collection of numbers, return all possible permutations. For example,...

[LeetCode]46.Permutations

【题目】 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have t...

LeetCode 46 Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations...

[LeetCode]--46. Permutations

Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have ...

Leetcode46 Permutations

Permutations Given a collection of numbers, return all possible permutations. For example, ...

Leetcode # 46. Permutations 全排列 解题报告

1 解题思想这道题是全排列,做法很多,而且Leetcode上随后也会有更多的变种这道题的假设是:distinct ,不重复,所以是最简单的一个,问题解法有两种: 1、设立一个状态数组,标示是否选择了...
  • MebiuW
  • MebiuW
  • 2016年04月17日 23:49
  • 567

LeetCode Algorithms 46. Permutations

题目难度: Medium 原题描述: Given a collection of distinct numbers, return all possible permu...

leetCode 46.Permutations(排列组合) 解题思路和方法

Permutations Given a collection of numbers, return all possible permutations. For example, [1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 46 Permutations
举报原因:
原因补充:

(最多只允许输入30个字)