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

有重复项数字的全排列

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


public class Solution {
    static ArrayList<ArrayList<Integer>>h=new ArrayList<>();
    public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
        Arrays.sort(num);
        ArrayList<Integer>a=new ArrayList<>();
        boolean[] now=new boolean[num.length];
        dg(num,a,now);
        return h;
    }
    private static void dg(int[] num, ArrayList<Integer> a,boolean[] now) {
        if (a.size()==num.length){
            if (h.contains(a)) {
                return;
            }else {

                h.add(new ArrayList<>(a));
                return;
            }
        }
        for(int i=0;i<num.length;i++){
            if (now[i]==true){
                continue;
            }
            now[i]=true;
            a.add(num[i]);
            dg(num,a,now);
            a.remove(a.size()-1);
            now[i]=false;
        }
    }
}
全部评论

相关推荐

04-08 23:37
已编辑
东华大学 结构工程师
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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