simple & short java solution


  • 0
    A
    public class Solution {
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            list.add(new ArrayList<Integer>());
            for (int i = 0; i < nums.length; i++) {  // for every number in the array
               int len = list.size();  // iterate current list elements
               for (int j = 0; j < len; j++) {  // the new number from arr can be added to any arr in list
                   ArrayList<Integer> tmp = new ArrayList<Integer>(list.get(j));
                   tmp.add(nums[i]);
                   list.add(tmp);
               } 
            }     
            return list;
        }
    }
    

Log in to reply
 

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