Commit b55d7630 authored by yaozc's avatar yaozc
Browse files

keep going

parent 27539330
......@@ -851,6 +851,17 @@ public:
Cut Off Trees for Golf Event
//这题凉了,很接近了但是tle解决不了
class Solution {
public:
int cutOffTree(vector<vector<int>>& forest) {
......@@ -908,7 +919,7 @@ public:
pair<int, int> curr = q.front();
q.pop();
visited[curr.first][curr.second] = true;
if(curr.first!=dest.first || curr.second!=dest.second) total_steps++;
if(curr.first==dest.first || curr.second!=dest.second) total_steps++;
else {return total_steps;}
//left one
if(curr.first-1>0 && forest[curr.first-1][curr.second]>1 && !visited[curr.first-1][curr.second])
......@@ -927,49 +938,6 @@ public:
}
};
//
/*#include <iostream>
#include <vector>
#include <string>
......@@ -1066,4 +1034,105 @@ cout<<cutOffTree(test);
return 0;
} */
\ No newline at end of file
} */
Letter Combinations of a Phone Number
//经典backtracking 问题 (其实就是个recursion问题啦,不难的,考虑好recursion的东西和条件就好了!)
class Solution {
public:
vector<string> dia = {"0", "1", "abc", "def", "ghi", "jkl", "nmo", "pqrs", "tuv", "wxyz" };
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits=="") return res;
helper(res, 0, "", digits);
return res;
}
void helper(vector<string> &current, int used_digit, string current_word, string digits){
if(used_digit == digits.size()){
current.push_back(current_word);
return;
}
else{
int current_digit = digits[used_digit]-'0';
for(int i=0; i<dia[current_digit].size(); i++){
current_word.push_back(dia[current_digit][i]);
helper(current, used_digit+1, current_word, digits);
// 非常重要!!!backtracking 的精华所在,一定要pop掉最后一个恢复到这个level应有的样子上去!!
current_word.pop_back();
}
}
}
};
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
if(n==0) return res;
vector<char> all_parentheses;
for(int i=0; i<n; i++){
all_parentheses.push_back('(');
all_parentheses.push_back(')');
}
helper(res,"", 0, all_parentheses);
return res;
}
void helper(vector<string>& res, string current_res, int current_used, vector<char>& all_parentheses){
if(current_used == all_parentheses.size()) res.push_back(current_res);
else{
for(int i=0; i<all_parentheses.size(); i++){
char add_one = all_parentheses[current_used];
current_res.push_back(add_one);
helper(res, current_res ,current_used+1, all_parentheses);
current_res.pop_back();
}
}
}
};
//
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