郑州外贸网站制作,有哪个理财网站是专门做汽车抵押的,腾讯云服务器收费标准,唐山长城网站建设给你一个整数数组 nums #xff0c;数组中的元素 互不相同 。返回该数组所有可能的子集#xff08;幂集#xff09;。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
#xff1a;总
// 注释的都为后来思考不必要的
class Solution {ListListInteger…给你一个整数数组 nums 数组中的元素 互不相同 。返回该数组所有可能的子集幂集。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
总
// 注释的都为后来思考不必要的
class Solution {ListListInteger res new ArrayList();ListInteger temp new ArrayList();public ListListInteger subsets(int[] nums) {// 加入空list// res.add(new ArrayList());// Arrays.sort(nums);backtrace(nums, 0);return res;}// start记录list中第一个元素下标for从start开始public void backtrace(int[] nums, int start){// if(!res.contains(temp)){ //contains复杂度nres.add(new ArrayList(temp));// return;// }for(int i start; i nums.length; i){// if(!temp.contains(nums[i])) {temp.add(nums[i]);// } else continue;backtrace(nums, i 1);temp.remove(temp.size() - 1);}}}
默写一遍加深印象
ListListInteger res new ArrayList();ListInteger temp new ArrayList();public ListListInteger subsets(int[] nums){backtrace(nums, start);return res;
}public void backtrace(int[] nums, int start){res.add(new ArrayList(temp));for(int i start; i nums.length; i){temp.add(nums[i]);backtrace(nums, i 1);temp.remove(temp.size() - 1);}
}