题解 | #有重复项数字的全排列#

有重复项数字的全排列

https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863

import java.util.*;
public class Solution {
    
    ArrayList<ArrayList<Integer>> ret;
    ArrayList<Integer> path;
    boolean[] check;

    public ArrayList<ArrayList<Integer>> permuteUnique (int[] num) {
        ret = new ArrayList<ArrayList<Integer>>();
        Arrays.sort(num);
        path = new ArrayList<Integer>();
        check = new boolean[num.length];
        dfc(num);

        return ret;
    }

    public void dfc(int[]num){

        //终止条件
        if(num.length==path.size()){
		  //没有该全排列时再加入
            if(!ret.contains(new ArrayList<>(path))){
                ret.add(new ArrayList<>(path));
            }
            
            return;
        }

        for(int i=0;i<num.length;i++){
            //当没有加入过该节点时
            if(check[i]==false){
                //加入
                path.add(num[i]);
                check[i] = true;
                //进入下一层加入
                dfc(num);
                //到最后回溯
                check[i] = false;
                path.remove(path.size()-1);
            }
        }
    }

}

全部评论

相关推荐

11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务