• [Leetcode 64] 78 Subsets


    Problem:

    Given a set of distinct integers, S, 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 S = [1,2,3], a solution is:

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

    Analysis:

    Simple backtracking problem.

    Code:

     1 class Solution {
     2 public:
     3     vector<vector<int> > res;
     4 
     5     vector<vector<int> > subsets(vector<int> &S) {
     6         // Start typing your C/C++ solution below
     7         // DO NOT write int main() function
     8         res.clear();
     9         sort(S.begin(), S.end());
    10         vector<int> path;
    11         for (int i=0; i<=S.size(); i++)
    12             bc(0, path, S, i);
    13         
    14         return res;
    15     }
    16     
    17     void bc(int idx, vector<int> path, vector<int> &s, int setSize) {
    18         if (path.size() == setSize) {
    19             res.push_back(path);
    20             return ;
    21         }
    22         
    23         for (int i=idx; i<s.size(); i++) {
    24             path.push_back(s[i]);
    25             bc(i+1, path, s, setSize);
    26             path.pop_back();
    27         }
    28         
    29         return ;
    30     }
    31 };
    View Code
  • 相关阅读:
    计算机语言发展简史
    HTML—xhtml和html5
    网络协议模型【简图】
    http协议
    URL简介
    TCP协议简介
    比较浏览器的“刷新”
    loadrunner之运行方式:线程还是进程?
    LoadRunner的函数
    LoadRunner测试结果分析
  • 原文地址:https://www.cnblogs.com/freeneng/p/3100028.html
Copyright © 2020-2023  润新知