[LeetCode 46 & 47] Permutations I & II

题目链接:permutations

相似题型:

1. [LeetCode 39&40] Combination Sum I & II

2. [LeetCode 78] Subsets

3. [LeetCode 90] Subsets II

4. [LeetCode 22] Generate Parentheses

5. [LeetCode 77] Combinations

import java.util.ArrayList;
import java.util.List;

/**
 *
		Given a collection of 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].
 *
 */

public class Permutations {

//	25 / 25 test cases passed.
//	Status: Accepted
//	Runtime: 244 ms
//	Submitted: 0 minutes ago

	//时间复杂度O(n!) 空间复杂度O(n)
	public List<List<Integer>> permutations = new ArrayList<List<Integer>>();
	public List<List<Integer>> permute(int[] num) {
        List<Integer> nums = new ArrayList<Integer>();
        for (Integer i : num) {
			nums.add(i);
		}
    	permute(nums, new ArrayList<Integer>());
    	return permutations;
    }
	public void permute(List<Integer> nums, List<Integer> permutation) {
    	if(nums.size() == 0) {
    		permutations.add(permutation);
    		return;
    	}
    	for (int i = 0; i < nums.size(); i++) {
			List<Integer> list1 = new ArrayList<Integer>(nums);
			List<Integer> list2 = new ArrayList<Integer>(permutation);
			list2.add(nums.get(i));
			list1.remove(i);
			permute(list1, list2);
		}
    }

	public static void main(String[] args) {

	}

}

题目链接:permutations-ii

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
		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].
 *
 */

public class PermutationsII {

//	30 / 30 test cases passed.
//	Status: Accepted
//	Runtime: 301 ms
//	Submitted: 0 minutes ago

	//时间复杂度O(n!) 空间复杂度O(n)
	public List<List<Integer>> permutations = new ArrayList<List<Integer>>();
	public List<List<Integer>> permuteUnique(int[] num) {
        List<Integer> nums = new ArrayList<Integer>();
        Arrays.sort(num);
        for (Integer i : num) {
			nums.add(i);
		}
        permuteUnique(nums, new ArrayList<Integer>());
    	return permutations;
    }
	public void permuteUnique(List<Integer> nums, List<Integer> permutation) {
    	if(nums.size() == 0) {
    		permutations.add(permutation);
    		return;
    	}
    	int pre = Integer.MAX_VALUE;
    	for (int i = 0; i < nums.size(); i++) {
    		if(nums.get(i) == pre) {
    			continue;
    		}
			List<Integer> list1 = new ArrayList<Integer>(nums);
			List<Integer> list2 = new ArrayList<Integer>(permutation);
			list2.add(nums.get(i));
			pre = list1.remove(i);
			permuteUnique(list1, list2);
		}
    }  

	public static void main(String[] args) {

	}

}
时间: 2024-10-10 12:40:40

[LeetCode 46 & 47] Permutations I & II的相关文章

LeetCode 【47. 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], [2,1,1] ] 思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复

LeetCode 46:Permutations

Given a collection of 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]. 这题不知道出了什么意外,输入[0,1]在Eclipse上运行结果:[[0, 1], [1, 0]] LeetCode上运行结果:[[1],[0,1],

leetcode || 46、Permutations

problem: Given a collection of 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]. Hide Tags Backtracking 题意:给定一个序列,输出其所有的排列组合 thinking: (1)之前写过求一个

[Leetcode 46]全排列 Permutations 递归

[题目] Given a collection of distinct integers, return all possible permutations. 数组的组合情况. Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] [思路] 求子集,排列组合的数组题有固定模板. [代码] class Solution { public List<List<Integer>> p

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

[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]

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 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的

LeetCode 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] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求