Leetcode 78. Subsets (backtracking) 90 subset
Posted wz30
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 78. Subsets (backtracking) 90 subset相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章