A 3 ms Java solution realized in a back tracking way


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