22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
Solution: 深搜,使用栈来判断当前字符串是否合法。也可以直接根据左右括号的数量判断,只有当前左括号数量多于右括号数量时,可以插入新的右括号。
Code:
class Solution {
public:
vector<string> generateParenthesis(int n) {
stack<char> s;
string path = "";
vector<string> ans;
generateParenthesis(n, n, s, path, ans);
return ans;
}
private:
void generateParenthesis(int left, int right, stack<char>& s, string& path, vector<string>& ans){
if(left==0 && right==0){
ans.push_back(path);
return;
}
if(left>0){
s.push('(');
path.push_back('(');
generateParenthesis(left-1, right, s, path, ans);
path.pop_back();
s.pop();
}
if(right>0 && !s.empty() && s.top()=='('){
s.pop();
path.push_back(')');
generateParenthesis(left, right-1, s, path, ans);
path.pop_back();
s.push('(');
}
}
};
37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'
.
You may assume that there will be only one unique solution.
A sudoku puzzle...
Solution: 深搜,对于未填的空位有两种方式:一种是遍历相关联的格子找出所有能填的数(我实现的方式),另一个是对1-9逐一查找是否能填入空格。
Code:
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
solveSudoku(board, 0, 0);
}
private:
bool solveSudoku(vector<vector<char>>& board, int x, int y){
if(x==board.size()){
return true;
}
if(board[x][y]!='.') return solveSudoku(board, x+(y+1)/9, (y+1)%9);
bool v[10];
fill_n(v, 10, false);
//检查横向
for(int i=0; i<9; i++)
if(board[x][i]!='.')
v[board[x][i]-'0'] = true;
//检查纵向
for(int i=0; i<9; i++)
if(board[i][y]!='.')
v[board[i][y]-'0'] = true;
//检查方块
for(int i=(x/3)*3; i<(x/3)*3+3; i++)
for(int t=(y/3)*3; t<(y/3)*3+3; t++){
if(board[i][t]!='.')
v[board[i][t]-'0'] = true;
}
for(int i=1; i<10; i++){
if(!v[i]){
board[x][y] = i+'0';
if(solveSudoku(board, x+(y+1)/9, (y+1)%9))
return true;
}
}
board[x][y] = '.';
return false;
}
};
79. Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]word =
"ABCCED"
, -> returns
true
,
word =
"SEE"
, -> returns
true
,
word =
"ABCB"
, -> returns
false
.
Solution: 深搜。
Code:
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if(word.size()==0) return true;
for(int i=0; i<board.size(); i++){
for(int t=0; t<board[0].size(); t++){
if(board[i][t]==word[0]){
if(exist(board, i, t, word, 0)) return true;
}
}
}
return false;
}
private:
bool exist(vector<vector<char>>& board, int x, int y, string& word, int i){
//检查board(x,y)和word[i]是否匹配,匹配则可以继续下一步搜索
if(!isValid(x, y, board) || board[x][y]!=word[i]) return false;
if(i==word.size()-1) return true;
board[x][y] = '-';//去重
bool ans = exist(board, x+1, y, word, i+1) ||
exist(board, x, y+1, word, i+1)||
exist(board, x-1, y, word, i+1)||
exist(board, x, y-1, word, i+1);
board[x][y] = word[i];
return ans;
}
bool isValid(int x, int y, vector<vector<char>>& board){
if(x<board.size() && y<board[0].size()) return true;
return false;
}
};