Share simple recursive java solution


  • 9
    W

    my method is simple, try to output all combination of the list and skip the same integer.

    public class Solution {
    public List<List<Integer>> subsetsWithDup(int[] num) {
        Arrays.sort(num);
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        LinkedList<Integer> temp = new LinkedList<Integer>();
        Rec(num,result,temp,0);
        return result;
    }
    private static void Rec(int[] a,List<List<Integer>> result, LinkedList<Integer> temp, int current){
        result.add(new LinkedList(temp));
        for(int i=current;i<a.length;i++){
            if(i==current || a[i]!=a[i-1]){
                temp.add(a[i]);
                Rec(a,result,temp,i+1);
                temp.remove(temp.size()-1);
            }
        }
        return;
    }
    

    }


Log in to reply
 

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