标签:backtrac return rem etc leetcode color rev subsets length
using prev
class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] nums) { List<Integer> temp = new ArrayList<>(); back(temp,nums, 0); return res; } void back(List<Integer> temp,int[] nums, int prev){ List<Integer> list = new ArrayList<>(temp); //if(!res.contains(list)) res.add(list); for(int i = prev; i<nums.length; i++){ temp.add(nums[i]); back(temp, nums, i+1); temp.remove(temp.size()-1); } } }
// 90 subset2
sorting the array first and check the contains
class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); public List<List<Integer>> subsetsWithDup(int[] nums) { List<Integer> temp = new ArrayList<>(); Arrays.sort(nums); back(temp,nums, 0); return res; } void back(List<Integer> temp,int[] nums, int prev){ List<Integer> list = new ArrayList<>(temp); if(!res.contains(list)) res.add(list); for(int i = prev; i<nums.length; i++){ temp.add(nums[i]); back(temp, nums, i+1); temp.remove(temp.size()-1); } } }
Leetcode 78. Subsets (backtracking) 90 subset
标签:backtrac return rem etc leetcode color rev subsets length
原文地址:https://www.cnblogs.com/stiles/p/Leetcode78.html