• 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.

    Example:

    Input: nums = [1,2,3]
    Output:
    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]
    class Solution {
        public void dfs(List<List<Integer>> ans, ArrayList<Integer>temp, int pos, int[] nums) {
            if (pos == nums.length) {
                ans.add(new ArrayList<>(temp));
                return;
            }
            
            dfs(ans, temp, pos + 1, nums);
            temp.add(nums[pos]);
            dfs(ans, temp, pos + 1, nums);
            temp.remove(temp.size() - 1);
            
    
        }
        public List<List<Integer>> subsets(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> ans = new ArrayList<>();
            ArrayList<Integer> temp = new ArrayList<>();
            //ans.add(new ArrayList<>());
            dfs(ans, temp, 0, nums);
            return ans;
            
        }
    }
  • 相关阅读:
    scnner02 (nextLine)
    Scanner01
    Spring 框架 (初学)
    查询自己写了多少行代码
    jdbc事务
    jdbc(预编译插入数据)
    jdbc(java连接数据库)
    监听器扩展
    listener(监听器)
    Filter过滤器
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/12324427.html
Copyright © 2020-2023  润新知