Commit 682b82cf authored by yaozc's avatar yaozc
Browse files

keep going

parent ec61268b
......@@ -1413,6 +1413,173 @@ public:
return res;
}
};
Permutations
//solution1, 用swap,每个都会到第一个的原理, backtracking
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
helper(nums, , res);
return res;
}
void helper(vector<int> &nums, int &level ,vector<vector<int>>& res){
if(level == nums.size()) {res.push_back(nums); return;}
else{
for(int i=level; i<nums.size(); i++){
swap(nums[i], nums[level]);
helper(nums, level+1, res);
swap(nums[level], nums[i]);
}
}
}
};
//solution2, backtracking, 用了排列组合,往pool里取char的方式
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<int> current;
helper(res, nums, current);
return res;
}
void helper(vector<vector<int>>& res, vector<int>& pool, vector<int>& current){
if (!pool.size()) {res.push_back(current);}
else{
for(int i=0; i<pool.size(); i++){
current.push_back(pool[i]);
vector<int> temp (pool.begin(), pool.end());
temp.erase(temp.begin()+i);
helper(res, temp, current);
current.pop_back();
}
}
}
};
78. Subsets
// backtracking问题训练集! 同上一题
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
for(int i=0; i<nums.size()+1; i++){
vector<vector<int>> temp;
vector<int> current;
helper(i, nums, current, temp);
res.insert(res.end(), temp.begin(), temp.end());
}
return res;
}
void helper(int goal, vector<int>& pool, vector<int>& current, vector<vector<int>>& res){
if(current.size() == goal) {res.push_back(current); }
else{
for(int i=0; i<pool.size(); i++){
current.push_back(pool[i]);
vector<int> 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<int> &a, const vector<int> &b){
return a[1] > b[1];
}
};
int minMeetingRooms(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
priority_queue<vector<int>, vector<vector<int>>, compartor> pq;
if(intervals.empty()) return 0;
pq.push(intervals[0]);
int rooms = 1;
for(int i=1; i<intervals.size(); i++){
if(pq.top()[1] >intervals[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!
Please register or to comment