题解 | #集合的所有子集(二)#

集合的所有子集(二)

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

import java.util.*;


public class Solution {
    
    public class ComparaInteger implements Comparator<Integer> {
        @Override
        public int compare(Integer num1, Integer num2) {
            return num1 - num2;
        }
    }
    
    public class ComparaArrayList implements Comparator<ArrayList<Integer>> {
        @Override
        public int compare(ArrayList<Integer> arrayList1, ArrayList<Integer> arrayList2) {
            int len1 = arrayList1.size();
            int len2 = arrayList2.size();
            int p1 = 0;
            int p2 = 0;
            while (p1 < len1 && p2 < len2) {
                if (arrayList1.get(p1) < arrayList2.get(p2)) {
                    return -1;
                }
                else if (arrayList1.get(p1) > arrayList2.get(p2)) {
                    return 1;
                }
                else {
                    p1++;
                    p2++;
                }
            }
            if (len1 < len2) {
                return -1;
            }
            else if (len1 > len2) {
                return 1;
            }
            else {
                return 0;
            }
        }
    }
    
    public ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    public int len;
    
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型二维数组
     */
    public ArrayList<ArrayList<Integer>> subsets (int[] nums) {
        // write code here
        len = nums.length;
        ArrayList<Integer> arrayList = new ArrayList<>();
        process(nums, 0, arrayList);
        res.sort(new ComparaArrayList());
        return res;
    }
    
    public void process(int[] nums, int start, ArrayList<Integer> arrayList) {
        ArrayList<Integer> copyArrayList = new ArrayList<>();
        copyArrayList.addAll(arrayList);
        copyArrayList.sort(new ComparaInteger());
        if (!isContains(copyArrayList)) {
            res.add(copyArrayList);
        }
        if (start >= len) {
            return;
        }
        for (int i = start; i < len; i++) {
            arrayList.add(nums[i]);
            process(nums, i + 1, arrayList);
            arrayList.remove(arrayList.size() - 1);
        }
        return;
    }
    
    public boolean isContains(ArrayList<Integer> arrayList) {
        boolean bool = false;
        for (ArrayList tmpArrayList : res) {
            if (isEquals(tmpArrayList, arrayList)) {
                bool = true;
                break;
            }
        }
        return bool;
    }
    
    public boolean isEquals(ArrayList<Integer> arrayList1, ArrayList<Integer> arrayList2) {
        boolean bool = true;
        int len1 = arrayList1.size();
        int len2 = arrayList2.size();
        if (len1 != len2) {
            return false;
        }
        for (int i = 0; i < len1; i++) {
            if (arrayList1.get(i) != arrayList2.get(i)) {
                bool = false;
                break;
            }
        }
        return bool;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-27 10:52
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:48
点赞 评论 收藏
分享
kyw_:接好运
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务