491.递增子序列
此题的难点:
1,前提需要保留原有顺序
2,保证递增
3,保证去重
注意:
去重一定要有set的同时保证有顺序
代码:
1 void findSubsequences_trackBack(vector<int>& nums, int startIndex, vector<int>& path, vector<vector<int>>& result) 2 { 3 if (path.size() >= 2 ) 4 { 5 result.push_back(path); 6 } 7 8 unordered_set<int> selected; 9 for (int i = startIndex; i < nums.size(); i++) 10 { 11 if (selected.find(nums[i]) != selected.end() || (path.size() > 0 && nums[i] < path.back())) 12 { 13 continue; 14 } 15 selected.insert(nums[i]); 16 path.push_back(nums[i]); 17 findSubsequences_trackBack(nums, i + 1, path, result); 18 path.pop_back(); 19 } 20 21 } 22 vector<vector<int>> findSubsequences(vector<int>& nums) { 23 vector<vector<int>> result; 24 if (nums.size() == 0) return result; 25 26 vector<int>path; 27 findSubsequences_trackBack(nums, 0, path, result); 28 return result; 29 }
标签:排列,nums,46,随想录,back,vector,result,path,size From: https://www.cnblogs.com/smartisn/p/17527836.html