My Recursive Java Solution

  • 0
        public List<List<Integer>> subsets(int[] nums) {
            List<List<Integer>> result = new LinkedList<List<Integer>>();
            List<Integer> element = new LinkedList<Integer>();
            addAnElement(nums, result, element, 0);   //0 shows the next element to add has index 0
            return result;
        public void addAnElement(int[] nums, List<List<Integer>>result, List<Integer> element, int index){
        	while (index < nums.length){
        		List<Integer> copy = new LinkedList<Integer>(element);
        		copy.add(nums[index++]);   //add an element and increment the index
        		addAnElement(nums, result, copy, index);

Log in to reply

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