Java DP solution


  • 0
    S

    public class Solution {
    public List<List<Integer>> subsets(int[] nums) {

       int len = nums.length;
       Arrays.sort(nums);
    	List<List<List<Integer>>> dp = new ArrayList(len+1);
    	List<Integer> x = new ArrayList();
    	List<List<Integer>> dp0 = new ArrayList();
    	dp0.add(x);
    	dp.add(0, dp0);
    	
    	for(int i=1; i<= len; i++){
    		List<List<Integer>> dpi = new ArrayList();
    		for(int j=0; j<dp.get(i-1).size(); j++){
    			dpi.add(dp.get(i-1).get(j));
    			ArrayList<Integer> dpj = new ArrayList();
    			dpj.addAll(dp.get(i-1).get(j));
    			dpj.add(nums[i-1]);
    			dpi.add(dpj);
    		}
    		dp.add(i, dpi);
    	}
    	return dp.get(len);	
    }
    

    }


Log in to reply
 

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