Given a set of distinct integers, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

题目大意:给定一个没有重复元素的数组,输出所有子集。

解题思路:直接看代码。

public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null){
return res;
}
Arrays.sort(nums);
res.add(new ArrayList<>());
for(int i=0;i<nums.length;i++){
int currSize = res.size();
for(int j=0;j<currSize;j++){
List<Integer> tmp = new ArrayList<>(res.get(j));
tmp.add(nums[i]);
res.add(tmp);
}
}
return res;
}
}

Subsets —— LeetCode的更多相关文章

  1. Subsets [LeetCode]

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

  2. Subsets LeetCode总结

    Subsets 题目 Given a set of distinct integers, nums, return all possible subsets. Note: The solution s ...

  3. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  6. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. [LeetCode] Subsets 子集合

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

  8. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  9. 【leetcode】Subsets (Medium) ☆

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

随机推荐

  1. table转list

    DataTable数据集转换为List非泛型以及泛型方式 前言 DataTable是断开式的数据集合,所以一旦从数据库获取,就会在内存中创建一个数据的副本,以便使用.由于在实际项目中,经常会将 Dat ...

  2. 上机实践 - - 一个例子了解C/C++中指针与数组的区别

    本例子来自于<剑指Offer>(P37) 解答如下: size1:20 data1是一个数组,sizeof(data1)是求数组大小. 这个数组包含5个整数,每个整数4个字节,共20字节. ...

  3. Echarts使用随笔(1)-Echarts中markPoint的使用(静态、动态)-effect

    先看一段关于初始化Echart   js的使用  myChart = echarts.init(document.getElementById('mainChart'));         var o ...

  4. java中怎么进行字符串替换?

    String str = "test.doc"; String newStr = str.replaceAll("doc","html");

  5. Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的

    <案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...

  6. iOS-NSSDate的使用

    取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970];long long int date = (long long ...

  7. Missing iOS Distribution signing identity问题解决

    问题描述 打包上传APPStore  Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Develop证书 ...

  8. QQ弹窗代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. jquery 自定义tab

    <!DOCTYPE html> <html> <head> <script src="/jquery/jquery-1.11.1.min.js&qu ...

  10. Android编译输出 资源指定AAPT

    BoardConfigCommon.mk # Default Command lineSEC_DEV_APP_LOCAL_AAPT_FLAGS := -c sw360dpSEC_DEV_FRAMEWO ...