Java Backtracking Solution


  • 0
    M
    public class Solution {
        public List<List<Integer>> subsets(int[] nums) {
        	List<List<Integer>> res = new ArrayList<>();
        	List<Integer> list = new ArrayList<>();
        	subset_recursion(0, nums, res, list);
        	return res;
        }
        
        private void subset_recursion(int start, int[] nums, List<List<Integer>> res, List<Integer> list){
        	res.add(new ArrayList<Integer>(list));
        	for(int i=start; i<nums.length; i++){
        		list.add(nums[i]);
        		subset_recursion(i+1, nums, res, list);
        		list.remove(list.size()-1);
        	}
        }
    }
    

Log in to reply
 

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