My java answer using Stack


  • 0
    D

    public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    List<Integer> sub=new ArrayList<Integer>();
    result.add(sub);
    int len=nums.length;
    int count=1;
    while(count<=len){
    List<List<Integer>> tmp=new ArrayList<List<Integer>>();
    con(tmp,nums,0,count,new Stack<Integer>());
    for(int i=0;i<tmp.size();i++){
    result.add(tmp.get(i));
    }
    count++;
    }
    return result;
    }
    private void con(List<List<Integer>> tmp ,int[] nums,int currentIndex,int count,Stack<Integer> numbers){
    if(count == 0){
    tmp.add(new ArrayList<Integer>(numbers));
    return;
    }

    for(int i = currentIndex; i < nums.length-count+1; i++){
        numbers.push(nums[i]);
        con(tmp, nums, i+1, count-1, numbers);
        numbers.pop();
    }
    
        
    }
    

    }


Log in to reply
 

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