LeetCode 491. 遞增的子序列
阿新 • • 發佈:2020-08-25
題目描述連結:https://leetcode-cn.com/problems/increasing-subsequences/
基本思路:DFS+雜湊
LeetCode C++ 求解程式碼:
class Solution { public: set<vector<int>> record; vector<vector<int>>res; vector<vector<int>> findSubsequences(vector<int>& nums) {for(int i=0;i<nums.size();i++){ vector<int>ans; ans.push_back(nums[i]); dfs(nums,i,i,ans); } return res; } void dfs(vector<int>&nums,int i,int j,vector<int>ans){ if(j==nums.size()){ return;//遞迴結束條件 } bool flag=0; if(i<j){ for(int k=0;k<ans.size();k++){ if(ans[k]>nums[j]){ flag=1; } } if(!flag){ ans.push_back(nums[j]); if(record.find(ans)==record.end()){ res.push_back(ans); record.insert(ans); } } }for(int x=j+1;x<nums.size();x++){ dfs(nums,i,x,ans); } if(!flag) ans.pop_back(); } };