Easy Java solution with graph explanation


  • 2
    B

    0_1474690136076_subsedt.jpg

    public class Subsets {
    	public List<List<Integer>> subsets(int[] nums) {
    		int resultSize = 1;
    		List<List<Integer>> result = new ArrayList<List<Integer>>();
    		result.add(new ArrayList<Integer>());//Initially, there must be a empty set
    		for(int i = 0; i < nums.length; i ++){
    			for(int j = 0; j < resultSize; j++){
    				List<Integer> subSet = new ArrayList<Integer>(result.get(j));
    				subSet.add(nums[i]);
    				result.add(subSet);
    			}
    			resultSize *= 2;
    		}
            return result;
        }
    }
    

  • 0
    A

    very good, no recursive


Log in to reply
 

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