LeetCode 46 Permutations II

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

For example,

[1,1,2] have the following unique permutations:

[1,1,2][1,2,1],
and [2,1,1].

思路,使用字典序法,与http://blog.csdn.net/mlweixiao/article/details/38897499 代码除了函数名换了以外,没有任何改动

public class Solution {
	private void nextPermutation(int[] num) {
		int i;
		int cur = -1;
		int temp;
		// find the last increase sequence
		for (i = num.length - 1; i >= 1; i--) {
			if (num[i] > num[i - 1]) {
				cur = i - 1;
				break;
			}
		}

		// if the increase sequence exists,
		// swap the cur and the last one(bigger than it)
		if (cur != -1) {
			for (i = num.length - 1; i > cur; i--) {
				if (num[i] > num[cur]) {
					temp = num[cur];
					num[cur] = num[i];
					num[i] = temp;
					break;
				}
			}
		}

		for (i = cur + 1; 2 * i <= cur + num.length - 1; i++) {
			temp = num[i];
			num[i] = num[num.length - i + cur];
			num[num.length - i + cur] = temp;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<List<Integer>> permuteUnique(int[] num) {

		Arrays.sort(num);
		int[] temparray = new int[num.length];
		System.arraycopy(num, 0, temparray, 0, num.length);

		ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();

		for (;;) {
			List list = new ArrayList<Integer>();
			// 注意不能使用list.addAll(Arrays.asList(num));
			// 也不能使用Collection.addAll(list ,num);
			// Arrays.asList() 返回java.util.Arrays$ArrayList,
			// 而不是ArrayList。Arrays$ArrayList和ArrayList都是继承AbstractList,
			//remove,add等method在AbstractList中是默认throw
			// UnsupportedOperationException而且不作任何操作。
			//
			for (int i = 0; i < num.length; i++) {
				list.add(num[i]);
			}
			result.add(list);
			nextPermutation(num);
			if (Arrays.equals(num, temparray))
				break;
		}
		return result;
	}
时间: 2024-08-29 00:05:03

LeetCode 46 Permutations II的相关文章

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

[LeetCode][JavaScript]Permutations II

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. https://leetcode.com/problems/permutations

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

LeetCode 047 Permutations II

题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 代码如下: class Solution { public: vector

【LeetCode】Permutations II

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

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

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

leetCode 46. Permutations 回溯问题 | Medium

46. Permutations(全排列问题--回溯问题经典) 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],   [3,2,1] ] 题目大意:求一个序列的全排列. 思路:做排列

[C++]LeetCode: 120 Permutations II

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这道题和Permutations的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不