Cannot work in leetcode but seems to work on eclipse


  • 0
    T

    Hi guys,
    this is my solution

    public class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        helper(0, nums, new ArrayList<Integer>());
        return res;
    }
    
    public void helper (int ind, int[] nums, List<Integer> cur) {
        res.add(cur);
    
        List<Integer> temp = new ArrayList<Integer>(cur);
        for (int i = ind; i < nums.length; i++) {
            if (i == ind || nums[i] != nums[i - 1]) {
            temp.add(nums[i]);
            helper(i + 1, nums, temp);
            temp.remove(temp.size() - 1);
            } 
        }
    }
    

    }

    I think it is right, but when I run [1, 2, 2] the result is [[],[],[1],[1,2],[],[2]] which is so weird, could anyone tell me why?


Log in to reply
 

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