78. Subsets

来源:互联网 发布:淘宝限时特价 编辑:程序博客网 时间:2024/06/06 06:55

78. Subsets

  • 题目描述:Given a set of distinct integers, nums, return all possible subsets (the power set).

    Note: 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],  []]
  • 题目大意:给定一个包含不同数字的数组,输出他所有的子序列。

  • 思路:回溯

  • 代码:

    package Array;import java.util.ArrayList;import java.util.List;import java.util.TreeMap;/*** @Author OovEver* @Date 2017/11/28 10:10*/public class Solution {  public List<List<Integer>> subsets(int[] nums) {      List<List<Integer>> res = new ArrayList<>();      backtrack(res, new ArrayList<>(), nums, 0);      return res;  }  private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int start) {      res.add(new ArrayList<>(tempList));      for(int i=start;i<nums.length;i++) {          tempList.add(nums[i]);          backtrack(res, tempList, nums, i + 1);          tempList.remove(tempList.size() - 1);      }  }}

原创粉丝点击