zoukankan      html  css  js  c++  java
  • 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],
      []
    ]
    

    解题思路:

    1-n进行递归?

    1. class Solution {  
    2. private:  
    3.     vector<vector<int>> ret;  
    4.     vector<int> store;  
    5.     void deep(int level, vector<vector<int>> &ret,vector<int>& nums,int count,int index){  
    6.         if(level == 0) {ret.push_back(store);return;}  
    7.         if(level == nums.size()) {ret.push_back(nums);return;}  
    8.         if(count == level){ret.push_back(store);return;}  
    9.           
    10.         for(int i=index;i<nums.size();i++){  
    11.             store.push_back(nums[i]);  
    12.             deep(level,ret,nums,count+1,i+1);  
    13.             store.pop_back();  
    14.               
    15.         }  
    16.           
    17.           
    18.           
    19.     }  
    20.   
    21. public:  
    22.     vector<vector<int>> subsets(vector<int>& nums) {  
    23.           
    24.           
    25.         for(int i=0;i<=nums.size();i++){  
    26.             store.clear();  
    27.             deep(i,ret,nums,0,0);  
    28.         }  
    29.         return ret;  
    30.           
    31.     }  
    32. };  
  • 相关阅读:
    springboot 整合 memcached l
    文件处理工具类 l
    执行脚本工具类 l
    LRU l
    归并排序 l
    Redis 数据类型 l
    git 常用操作命令 唏嘘
    postmanPOST请求 status 415错误 唏嘘
    MySQL 启动和关闭MySQL服务 唏嘘
    OO和OP
  • 原文地址:https://www.cnblogs.com/liangyc/p/8847609.html
Copyright © 2011-2022 走看看