一、题目描述

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

二、思路

三、代码实现

 package cn.zifuchuan;

 import java.util.LinkedList;
import java.util.List; public class Test5 { public static void main(String[] args) {
int[] nums = {1,2,3};
List<List<Integer>> list = subsets(nums);
System.out.println(list);
}
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
dfs(res, new LinkedList<Integer>(), nums, 0); //从含有0个元素的情况开始分析
return res;
} public static void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int start) {
res.add(new LinkedList<Integer>(temp));
for (int i =start; i < nums.length; i++) {
temp.add(nums[i]);
dfs(res, temp, nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}

leetcode-数组-子集的更多相关文章

  1. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  2. LeetCode:子集 II【90】

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

  3. 每日一题-——LeetCode(78)子集

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  [1,2,3],  [1,3],  [2, ...

  4. Leetcode数组题*3

    目录 Leetcode数组题*3 66.加一 题目描述 思路分析 88.合并两个有序数组 题目描述 思路分析 167.两数之和Ⅱ-输入有序数组 题目描述 思路分析 Leetcode数组题*3 66.加 ...

  5. LeetCode 数组分割

    LeetCode 数组分割 LeetCode 数组怎么分割可以得到左右最大值的差值的最大 https://www.nowcoder.com/study/live/489/1/1 左右最值最大差 htt ...

  6. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  7. [leetcode]78. Subsets数组子集

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

  8. LeetCode 78. 子集(Subsets) 34

    78. 子集 78. Subsets 题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明: 解集不能包含重复的子集. 每日一算法2019/6/6Day 34L ...

  9. [LeetCode]78. 子集(位运算;回溯法待做)

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

  10. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

随机推荐

  1. ESP8266莫名重启或者死机问题

    多半是内存使用不当 1. 如果你要用很大长度的数组,那么可以换用更小的数据类型.比如,int值要占用两个字节,你可以用byte(只占用一个字节)代替:    2. esp8266有时会莫明重启,大部分 ...

  2. Uni-app事件处理

    事件表: Web事件 uni-app事件 click tap touchstart touchstart touchmove touchmove touchcancel touchcancel tou ...

  3. Django组件之用户认证组件

    一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenticate( ...

  4. C++的变量初始化

    C++中变量的初始化有很多种方式,如:默认初始化,值初始化,直接初始化,拷贝初始化,列表初始化. 1.默认初始化:默认初始化是指定义变量时没有指定初值时进行的初始化操作. 如:int a:这些变量被定 ...

  5. FreeRTOS 启动进程调度后,程序卡死的部分原因分析。

    现象:1,RTOS  使用时 系统卡启动文件               B       .处. 原因分析:该种情况是由于定义开启了中断,但是未开启中断处理服务.程序执行到中断响应式无对应的程序响应 ...

  6. Selenium WebDriver的实现及工作原理

    笔者最近研究学习了selenium的实现和工作原理,阅读了selenium3.141.59的Java源码,没有读完哈...重点从两个接口(org.openqa.selenium.WebDriver和o ...

  7. 用spark-submit启动程序

    来源:http://spark.apache.org/docs/latest/submitting-applications.html 提交程序常用的一些选项 ./bin/spark-submit \ ...

  8. 解决问题SyntaxError: Unexpected token import

    ES6语法的模块导入导出(import/export)功能,我们在使用它的时候,可能会报错: SyntaxError: Unexpected token import 语法错误:此处不应该出现impo ...

  9. 项目管理目标:添加人员并向其分配任务 - Project

    已剪辑自: https://support.office.com/zh-cn/article/%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86%E7%9B%AE%E6%A0%8 ...

  10. Linux内核优化

     相信做运维的同仁,进行运维环境初建时,必须要考虑到操作系统内核参数的优化问题,本人经历数次的运维环境重建后,决定要自行收集一份比较完善的系统内核参数优化说明文件出来,于是就有了下文,本文当前值是官方 ...