1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. /**
  5. * Source : https://oj.leetcode.com/problems/subsets/
  6. *
  7. *
  8. * Given a set of distinct integers, S, return all possible subsets.
  9. *
  10. * Note:
  11. *
  12. * Elements in a subset must be in non-descending order.
  13. * The solution set must not contain duplicate subsets.
  14. *
  15. * For example,
  16. * If S = [1,2,3], a solution is:
  17. *
  18. * [
  19. * [3],
  20. * [1],
  21. * [2],
  22. * [1,2,3],
  23. * [1,3],
  24. * [2,3],
  25. * [1,2],
  26. * []
  27. * ]
  28. */
  29. public class SubSet {
  30. private List<List<Integer>> result = new ArrayList<List<Integer>>();
  31. /**
  32. *
  33. *
  34. * @return
  35. */
  36. public List<List<Integer>> subset (int[] arr) {
  37. List<Integer> set = new ArrayList<Integer>();
  38. Arrays.sort(arr);
  39. recursion(arr, 0, set);
  40. return result;
  41. }
  42. private void recursion (int[] arr, int index, List<Integer> set) {
  43. if (index == arr.length) {
  44. return;
  45. }
  46. for (int i = index; i < arr.length; i++) {
  47. // if (i < arr.length - 1 && arr[i] == arr[i+1]) {
  48. // continue;
  49. // }
  50. set.add(arr[i]);
  51. List<Integer> temp = new ArrayList<Integer>(set);
  52. result.add(temp);
  53. recursion(arr, i + 1, set);
  54. set.remove(set.size()-1);
  55. }
  56. }
  57. private static void print (List<List<Integer>> list) {
  58. for (List<Integer> arr : list) {
  59. System.out.println(Arrays.toString(arr.toArray(new Integer[arr.size()])));
  60. }
  61. System.out.println();
  62. }
  63. public static void main(String[] args) {
  64. SubSet subSet = new SubSet();
  65. int[] arr = new int[]{1,2,3};
  66. print(subSet.subset(arr));
  67. }
  68. }

leetcode — subsets的更多相关文章

  1. LeetCode:Subsets I II

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

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

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

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

  4. [LeetCode] Subsets 子集合

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

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  7. [leetcode]Subsets @ Python

    原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @ ...

  8. [Leetcode] Subsets II

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

  9. [LeetCode] Subsets (bfs的vector实现)

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

随机推荐

  1. Python序列化proto中repeated修饰的数据

    一.repeated修饰复合数据结构,即message时 1.使用message的add方法初始化新实例 2.分别对新实例中的每个元素赋值:或使用CopyFrom(a)拷贝a中的元素值 message ...

  2. Exp5 MSF基础应用 20164302 王一帆

    1.实验内容 1.1实验要求 一个主动攻击实践,ms08_067; 一个针对浏览器的攻击,MS10-018(成功且唯一),MS12-004(成功且唯一): 一个针对客户端的攻击,adobe_toolb ...

  3. 蛤?你要用html做游戏?(笔记版)

    标签(空格分隔):canvas html game 本书是看<html5 Canvas游戏开发实战>(2013)笔记 博主小白,啥也不懂类型,这只是一个笔记,需要的话可以看原书. 书张这样 ...

  4. 接口测试——postman & jmeter

    新名词: 自动化测试:写代码帮你测试 接口:是一个抽象的概念,一种交互关系. 抓包:拦截请求. 接口测试:就是功能测试,比后者还简单. 需要有测试文档,包括项目.模块.URL.请求方式.参数.参数说明 ...

  5. remote: HTTP Basic: Access denied fatal: Authentication failed for'https'

    问题原因: 重置了密码导致git操作失败. 解决方案: 输入:git config --system --unset credential.helper 再次进行git操作,输入用户名,密码.

  6. 用gulp-imageisux智图api压缩图片

    ➣ 智图平台是什么? 智图是腾讯ISUX前端团队开发的一个专门用于图片压缩和图片格式转换的平台,其功能包括针对png,jpeg,gif等各类格式图片的压缩,以及为上传图片自动选择最优的图片格式.同时, ...

  7. Nginx如何对日志文件进行配置?

    在我们日常工作开发中,对调试bug最重要的手段就是查看日志和断点调试了. 今天我们来说日志文件,Nginx的日志文件一般保存的是访问日志和错误日志. 1. 用来log_format指令设置日志格式 l ...

  8. java课程课后作业190425之一维数组最大子数组(界面实现)

    题目要求: 1.在第一个问题过程中,我在以前的代码中好像已经写出了这个功能,想要实现这个功能,我们只需要在我们储存的数组和是负数的时候对中转值进行重新赋值就可以得到新的数值的起始位置,而他的终了位置就 ...

  9. truffle unbox react 出坑指南

    最近几天差点就被这鬼东西给逼疯了,truffle init .truffle unbox webpack 不管我怎么运行都是对的,唯独truffle unbox react 不管在哪个windows都 ...

  10. TCP协议学习总结(中)

    很多人都说TCP协议是一个十分复杂的协议,在学习当中,我对协议每一个问题都分解学习后,每一个分解我都能体会和理解它的要点,并不难理解.但我把这些拆分的细节合并后,确认感觉这样一个协议相对“臃肿”但又好 ...