Commit 682b82cf by yaozc

### keep going

parent ec61268b
 ... ... @@ -1413,6 +1413,173 @@ public: return res; } }; Permutations //solution1, 用swap，每个都会到第一个的原理, backtracking class Solution { public: vector> permute(vector& nums) { vector> res; helper(nums, , res); return res; } void helper(vector &nums, int &level ,vector>& res){ if(level == nums.size()) {res.push_back(nums); return;} else{ for(int i=level; i> permute(vector& nums) { vector> res; vector current; helper(res, nums, current); return res; } void helper(vector>& res, vector& pool, vector& current){ if (!pool.size()) {res.push_back(current);} else{ for(int i=0; i temp (pool.begin(), pool.end()); temp.erase(temp.begin()+i); helper(res, temp, current); current.pop_back(); } } } }; 78. Subsets // backtracking问题训练集！ 同上一题 class Solution { public: vector> subsets(vector& nums) { vector> res; for(int i=0; i> temp; vector current; helper(i, nums, current, temp); res.insert(res.end(), temp.begin(), temp.end()); } return res; } void helper(int goal, vector& pool, vector& current, vector>& res){ if(current.size() == goal) {res.push_back(current); } else{ for(int i=0; i temp(pool.begin()+i+1, pool.end()); helper(goal, temp, current, res); current.pop_back(); } } } }; Meeting Rooms II //priority_quueue 真的很实用很重要！尤其是在只考虑当前最小或者最大的时候！ class Solution { public: struct compartor{ bool operator() (const vector &a, const vector &b){ return a[1] > b[1]; } }; int minMeetingRooms(vector>& intervals) { sort(intervals.begin(), intervals.end()); priority_queue, vector>, compartor> pq; if(intervals.empty()) return 0; pq.push(intervals[0]); int rooms = 1; for(int i=1; iintervals[i][0]) rooms++; else{ pq.pop(); } pq.push(intervals[i]); } return rooms; } }; //Subsets II (contains duplicates) : https://leetcode.com/problems/subsets-ii/ // backtracking问题训练集！ //Permutations : https://leetcode.com/problems/permutations/ // backtracking问题训练集！ //Permutations II (contains duplicates) : https://leetcode.com/problems/permutations-ii/ // backtracking问题训练集！ //Combination Sum : https://leetcode.com/problems/combination-sum/ // backtracking问题训练集！ //Combination Sum II (can't reuse same element) : https://leetcode.com/problems/combination-sum-ii/ // backtracking问题训练集！ //Palindrome Partitioning : https://leetcode.com/problems/palindrome-partitioning/ // backtracking问题训练集！ ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!