leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0491.cpp (622B)


      1 class Solution {
      2     vector<vector<int>> res;
      3     vector<int> seq;
      4 
      5   public:
      6     vector<vector<int>> findSubsequences(vector<int> &nums) {
      7         dfs(nums, 0);
      8         return res;
      9     }
     10 
     11     void dfs(vector<int> &nums, int pos) {
     12         if (seq.size() > 1) res.push_back(seq);
     13         unordered_set<int> hash;
     14         for (int i = pos; i < nums.size(); i++) {
     15             if ((seq.empty() || nums[i] >= seq.back()) && !hash.count(nums[i])) {
     16                 seq.push_back(nums[i]);
     17                 dfs(nums, i + 1);
     18                 seq.pop_back();
     19                 hash.insert(nums[i]);
     20             }
     21         }
     22     }
     23 };