题目:矩阵置0

难度:Easy

题目内容

 

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

翻译

给定一组不同的整数,nums,返回所有可能的子集(包括空集和自身)。

注意:解决方案集不能包含重复的子集。

Example:

Input: nums = [1,2,3]
Output:
[
[3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

我的思路:无。。。。。

答案代码

 public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}

答案复杂度:O(n2

答案思路

其实就是利用回溯递归的思想,单独写一个回溯方法,对每一个子集都是由各个字母开头组成的(子集的子集的子集。。。)这样一个集合,

所以方法内只需要先将tempList加入结果集List,然后写一个循环,将从start开始,依次将当前nums[ i ] 加入tempList,然后递归调用回溯方法(start= i+1),调用完毕后说明以nums[ i ]开头的子集结束,所以将nums[ i ]从tempList中移出。

下面是流程:以输入【1,2,3】为例(在回溯方法第一行打印tempList即可看见)

[]———————从空开始
[1]——————以空开头的第一个
[1, 2]——————以1开头的第一个
[1, 2, 3]——————以12开头的第一个,此时到3了,说明以12开头的子集结束,删掉2,此时回溯到以1开头
[1, 3]——————以1开头的第二个,此时又到3,以1开头的子集结束,删掉1,回溯到以空开头
[2]——————以空开头的第二个
[2, 3]——————以2开头的第一个,此时到3,以2开头的子集结束,删掉2,回溯到以空开头
[3]——————以空开头的最后一个

扩展:当nums包含重复字符的时候应该怎么办?第[90]题:Subsets 2

1、一开始是乱序的所以需要将nums排序才能判断是否重复。

2、在回溯方法的循环里第一行加入重复判断,如果当前元素(非第一个)和上一个元素一样,那么就跳过此元素不使用递归。

 public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}

LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2的更多相关文章

  1. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  2. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  3. [Leetcode 78]求子集 Subset

    [题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  7. LeetCode(78):子集

    Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...

  8. 【LeetCode】78. Subsets (2 solutions)

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  9. java面试题及答案(基础题122道,代码题19道)

    JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...

随机推荐

  1. 初识Java集合框架(Iterator、Collection、Map)

    1. Java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 注意: 既有接口也有类,图中画实线的是类,画虚线的是接口 使用之前须要到导入java.util包 List ...

  2. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  3. sublime2常用插件

    Package Control 1. 安装方法 • 下载地址:https://sublime.wbond.net/Package%20Control.sublime-package • 将下载下来的P ...

  4. Laravel 文档中的 Service Providers

    $this->app->singleton('ReportServices', function () { return new \App\Services\ReportServices( ...

  5. CodeForces - 528D Fuzzy Search (FFT求子串匹配)

    题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...

  6. NodeJS NPM HTTPS

    npm config set registry http://registry.npmjs.org/

  7. HTML5文件上传前本地预览

    HTML5之FileReader的使用 HTML5定义了FileReader作为文件API的重要成员用于读取文件,根据W3C的定义,FileReader接口提供了读取文件的方法和包含读取结果的事件模型 ...

  8. 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示

    在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...

  9. Javascript中的函数数学运算

    1.Math函数与属性使用语法 Math.方法名(参数1,参数2,...); Math.属性; 说明 Math函数可以没有参数,比如Math.random()函数,或有多个参数,比如Math.max( ...

  10. Cloudera Manager安装之时间服务器和时间客户端(二)

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...