LeetCode 47 Permutations II(全排列)

题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description

给出数组,数组中的元素可能有重复,求出所有的全排列

使用递归算法:

传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used

其中list保存最终结果

tempList保存其中一个全排列组合

nums保存初始的数组

used随着计算不断进行更新操作,记录所有数字是否已经使用

参考代码:

package leetcode_50;

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

/***
 *
 * @author pengfei_zheng
 * 给定数组元素可能重复,求出所有全排列
 */
public class Solution47 {
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        if(nums==null || nums.length==0) return ans;
        boolean[] used = new boolean[nums.length];
        List<Integer> list = new ArrayList<Integer>();
        Arrays.sort(nums);
        prem(ans,list,nums,used);
        return ans;
    }

    private static void prem(List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used) {
        if(tempList.size()==nums.length){
            list.add(new ArrayList<>(tempList));
        }
        else{
            for(int i = 0; i < nums.length; i++){
                if(used[i] || i>0 && nums[i]==nums[i-1] && !used[i-1]) continue;
                used[i]=true;
                tempList.add(nums[i]);
                prem(list,tempList,nums,used);
                used[i]=false;
                tempList.remove(tempList.size()-1);
            }
        }
    }
    public static void main(String[]args){
        int []nums={3,0,3,3};
        List<List<Integer>> list = permute(nums);
        for(List<Integer> item:list){
            System.out.println(item);
        }
    }
}
时间: 2024-12-07 01:24:36

LeetCode 47 Permutations II(全排列)的相关文章

[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

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

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] ] 与上一题不同,就是在19行加个判断即可. 1 class Solution(object): 2 def __ini

19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&

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的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,

[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

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,当前排列数

[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的区别就是,输入的数字数组中包含重复的元素.如果我们对重复的元素不