Subsets

简介: 给定一个int数组,找出所有的子集;结果要排好序 Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order.

给定一个int数组,找出所有的子集;结果要排好序

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],
  []
]

Java代码:

 1 package com.rust.datastruct;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collections;
 5 import java.util.List;
 6 
 7 class SubsetsSolution {
 8     public List<List<Integer>> subsets(int[] nums) {
 9         List<List<Integer>> res = new ArrayList<List<Integer>>();
10         List<Integer> temp = new ArrayList<Integer>();
11         res.add(temp);/* the empty subset: [] */
12         findSubsets(nums, 0, temp, res);
13         return res;
14     }
15     private void findSubsets(int nums[],int start, List<Integer> singleSet,
16             List<List<Integer>> result){
17         if (start >= nums.length){
18             return;
19         }
20         for (int i = start; i < nums.length; i++) {
21             /*keep singleSet*/
22             List<Integer> subset = new ArrayList<Integer>(singleSet);
23             subset.add(nums[i]);
24             Collections.sort(subset);
25             result.add(subset);/*put in result*/
26             findSubsets(nums, i + 1, subset, result);
27         }
28     }
29 }
30 public class Subsets {
31     private static SubsetsSolution solution;
32     private static List<List<Integer>> res;
33     public static void main(String args[]){
34         int nums[] = {1,4,3,2};
35         solution = new SubsetsSolution();
36         res = solution.subsets(nums);
37         for (int i = 0; i < res.size(); i++) {
38             System.out.println(res.get(i));
39         }
40     }
41 }

输出:

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

递归处理问题。从输出结果可以看出处理的流程。

找到第一个数,这里是“1”,然后“1”保持不动,找下一个数“4”。然后“1”和“4”不动,再接着找。

每个元素都有机会作为开头的数。从左往右遍历一次,每次都会找当前数的右边的可能组合。

目录
相关文章
|
数据可视化 机器人 C++
Understanding nodes:理解节点(nodes)
Understanding nodes:理解节点(nodes)
145 0
Understanding nodes:理解节点(nodes)
LeetCode 90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
80 0
LeetCode 90. Subsets II
|
索引
LeetCode 368. Largest Divisible Subset
给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0。 如果有多个目标子集,返回其中任何一个均可。
77 0
LeetCode 368. Largest Divisible Subset
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
74 0
LeetCode 329. Longest Increasing Path in a Matrix
|
Android开发
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
131 0
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
Yet Another Two Integers Problem
Yet Another Two Integers Problem
104 0
Yet Another Two Integers Problem