Subsets vs. Subsets II: Add only 3 more lines to Subsets solution
The solution of subset II could be easily derived from the answer of subset I. \n\nHere is my answer of subset I: \n\npublic class Solution {\n    public List<L
The solution of subset II could be easily derived from the answer of subset I. \n\nHere is my answer of subset I: \n```\npublic class Solution {\n    public List<List<Integer>> subsets(int[] nums) {\n        List<List<Integer>> res = new ArrayList<>(); \n        res.add(new ArrayList<>()); \n        \n        for (int num: nums) {\n            List<List<Integer>> resDup = new ArrayList<>(res);\n            for (List<Integer> list:resDup) {\n                List<Integer> tmpList = new ArrayList<>(list);\n                list.add(num);\n                res.add(tmpList); \n            }\n        }\n        return res; \n    }\n}\n```\n\nIn this problem, we need to change two things: \n1. Sort the input nums, so that we won't get lists such as [1,4] and [4, 1] at the same time. \n2. Check duplicates when adding new list to res. \nHere is Subset II solution based on subset I solution: \n```\npublic class Solution {\n    public List<List<Integer>> subsetsWithDup(int[] nums) {\n        List<List<Integer>> res = new ArrayList<>();\n        res.add(new ArrayList<Integer>()); \n        Arrays.sort(nums); //important: sort nums \n        \n        for (int num: nums) {\n            List<List<Integer>> resDup = new ArrayList<>(res);\n            for (List<Integer> list: resDup) {\n                List<Integer> tmp = new ArrayList<>(list);\n                tmp.add(num);\n                if (!res.contains(tmp))  //check duplicates\n                    res.add(tmp);\n            }\n        }\n        return res; \n    }\n}\n```
[]