LeetCode_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 = [
["ABCE"],
["SFCS"],
["ADEE"]
] word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

  AC 代码:

class Solution {
public:
bool search(vector<vector<char> > &board, const string & word, int i, int j, int index, vector<vector<bool>> &flag)
{
if(index == len) return true;
//up
if(i- >= && board[i-][j] == word[index] && flag[i-][j] == false) {
flag[i-][j] = true;
if(search(board, word, i-, j,index+, flag) ) return true;
flag[i-][j] = false;
}
//down
if(i+ < rows && board[i+][j] == word[index] && flag[i+][j] == false){
flag[i+][j] = true;
if(search(board, word, i+, j, index+, flag)) return true;
flag[i+][j] = false;
}
//right
if( j+ < columns && board[i][j+] == word[index] && flag[i][j+] == false){
flag[i][j+] = true;
if(search(board, word, i, j+, index+, flag)) return true;
flag[i][j+] = false;
}
//left
if(j- >= && board[i][j-] == word[index] && flag[i][j-] == false){
flag[i][j-] = true;
if(search(board, word, i, j-, index+, flag)) return true;
flag[i][j-] = false;
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
columns = board[].size();
len = word.size();
if(len == ) return true ;
if(rows * columns < len) return false; for(int i = ; i< rows ;i++)
for(int j = ; j< columns ; j++){
if(board[i][j] == word[])
{
vector<vector<bool>> flag(rows, vector<bool>(columns, false)) ;
flag[i][j] = true;
if(search(board,word, i, j,,flag)) return true;
// flag[i][j] = false;
}
} return false;
}
private:
int rows;
int columns;
int len ;
};

未AC的奇怪代码

class Solution {
public:
bool search(vector<vector<char> > &board, const string & word, int i, int j, int index)
{
if(index == len) return true;
//up
if(i- >= && board[i-][j] == word[index] && flag[i-][j] == false) {
flag[i-][j] = true;
if(search(board, word, i-, j,index+) ) return true;
flag[i-][j] = false;
}
//down
if(i+ < rows && board[i+][j] == word[index] && flag[i+][j] == false){
flag[i+][j] = true;
if(search(board, word, i+, j, index+)) return true;
flag[i+][j] = false;
}
//right
if( j+ < columns && board[i][j+] == word[index] && flag[i][j+] == false){
flag[i][j+] = true;
if(search(board, word, i, j+, index+)) return true;
flag[i][j+] = false;
}
//left
if(j- >= && board[i][j-] == word[index] && flag[i][j-] == false){
flag[i][j-] = true;
if(search(board, word, i, j-, index+)) return true;
flag[i][j-] = false;
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
rows = board.size();
columns = board[].size();
len = word.size();
if(len == ) return true ;
if(rows * columns < len) return false;
flag.resize(rows, vector<bool>(columns, false)) ;
for(int i = ; i< rows ;i++)
for(int j = ; j< columns ; j++){
if(board[i][j] == word[])
{
flag[i][j] = true;
if(search(board,word, i, j,)) return true;
flag[i][j] = false;
}
} return false;
}
private:
vector<vector<bool>> flag;
int rows;
int columns;
int len ;
};
上一篇:传说中的WCF(2):服务协定的那些事儿


下一篇:python 中去除BOM头