47. 全排列 II

LeetCode: 47. 全排列 II

在这里插入图片描述



超时代码

    List<List<Integer>> list = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    final int maxInt = Integer.MAX_VALUE;

    public List<List<Integer>> permuteUnique(int[] nums) {
        getRes(nums, temp);
        return list;
    }


    public void getRes(int[] nums, List<Integer> t){

        if(t.size() == nums.length){
            for (List<Integer> lis: list) {
                if(lis.hashCode() == t.hashCode()){
                    return ;
                }
            }
            list.add(new ArrayList<>(t));

            return ;
        }

        for (int i = 0; i < nums.length; i++) {

            if(nums[i] != maxInt){
                temp.add(nums[i]);
                nums[i] = maxInt;
                getRes(nums, temp);
                nums[i] = temp.remove(temp.size() - 1);
            }

        }
        return ;
    }

在这里插入图片描述



提交代码


class Solution {
    // 1 1 2

    List<List<Integer>> list = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    String str = "";
    final int maxInt = Integer.MAX_VALUE;

    public List<List<Integer>> permuteUnique(int[] nums) {
        getRes(nums, temp);
        return list;
    }


    public void getRes(int[] nums, List<Integer> t){
        if(t.size() == nums.length){
            if(str.contains(t.toString())){ return ; }
            list.add(new ArrayList<>(t));
            str += t.toString();

            return ;
        }

        for (int i = 0; i < nums.length; i++) {

            if(nums[i] != maxInt){
                temp.add(nums[i]);
                nums[i] = maxInt;
                getRes(nums, temp);
                nums[i] = temp.remove(temp.size() - 1);
            }

        }
        return ;
    }
}


在这里插入图片描述



优化



    List<List<Integer>> list = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();
    private final int maxInt = Integer.MAX_VALUE;

    public List<List<Integer>> permuteUnique(int[] nums) {
    // 排序
        Arrays.sort(nums);
        getRes(nums, temp);
        return list;
    }


    public void getRes(int[] nums, List<Integer> t) {
        if (t.size() == nums.length) {
            list.add(new ArrayList<>(t));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
        // 剪枝条件
            if (nums[i] == maxInt || (i > 0 && nums[i] == nums[i - 1] && nums[i - 1] != maxInt)) {
                continue;
            }
            
            //if(nums[i] != maxInt)
            temp.add(nums[i]);
            nums[i] = maxInt;
            getRes(nums, temp); 
       		// 递归回溯
            nums[i] = temp.remove(temp.size() - 1);

        }
        return;
    }


    public static void main(String[] args) {
        int[] n = {3, 3, 0, 3};

        Daily47 d = new Daily47();
        List<List<Integer>> lists = d.permuteUnique(n);

        System.out.println(Arrays.toString(lists.toArray()));
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值