Java concise backtrack solution


  • 0
    J
        private List<List<Integer>> ans = new ArrayList<List<Integer>>();
        public List<List<Integer>> subsets(int[] nums) {
            int start = 0, end = nums.length;
            List<Integer> sub = new ArrayList<Integer>();
            helper(nums,sub,start,end);
            return ans;
        }
        public void helper(int[] nums,List<Integer> sub, int start, int end){
                List<Integer> new_sub = new ArrayList<Integer>(sub);
                ans.add(new_sub);
            for(int i = start; i < end; i++){
                sub.add(nums[i]);
                helper(nums, sub, i+1, end);
                sub.remove(sub.size() - 1);
            }
            return;
        }

Log in to reply
 

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