Accepted Java iterative solution


  • 0
    M
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        
        int index = 0;
        List<List<Integer>> ss = new ArrayList<>();
        
        List<Integer> e = new ArrayList<>();
        ss.add(e);
        
        for(int i=0;i<nums.length;i++){
            
            if(i==0){
                e = new ArrayList<>();
                e.add(nums[i]);
                index = ss.size();
                ss.add(e);
                continue;
            }
            int start = (nums[i-1]==nums[i])?index:0;
            index = ss.size();
            
            for(int j=start;j<index;j++){
                e = new ArrayList(ss.get(j));
                e.add(nums[i]);
                ss.add(e);
            }
        }
        return ss;
    }

Log in to reply
 

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