Backtracking Java Solution Easy to Understand


  • 0
    A
    public class Solution {
        
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        public List<List<Integer>> subsets(int[] nums) {
            List<Integer> list = new ArrayList<Integer>();
            helper(nums, list, 0);
            return result;
            
        }
        
        public void helper(int[] nums, List<Integer> list, int index){
            result.add(new ArrayList<Integer>(list));
            if(index == nums.length){
                return;
            }
            
            for(int i=index;i<nums.length;i++){
                list.add(nums[i]);
                index += 1;
                helper(nums, list, index);
                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.