Simple Java Solution


  • 0
    Z
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Solution {
        public List<List<Integer>> subsetsWithDup(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
            result.add(new ArrayList<>());
            for (int i = 0; i < nums.length; i++) {
                List<List<Integer>> tmp = new ArrayList<>();
                for (List<Integer> res : result) {
                    List<Integer> list = new ArrayList<>(res);
                    list.add(nums[i]);
                    if(!result.contains(list)) {
                        tmp.add(list);
                    }
                }
                result.addAll(tmp);
    
            }
    
            return result;
        }
     
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.