leetcode 079 —— Word Search
2015-08-03 12:10
399 查看
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 =
word =
-> returns
word =
-> returns
word =
-> returns
思路: 思路,用一个矩阵记录被使用的单词,但是代码跑了44ms,还有优化的空间
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
bool judge = false;
int level = 0;
vector<vector<bool>> record(board.size(), vector<bool>(board[0].size(), false));
for (int m = 0; m <board.size(); m++){
for (int n = 0; n <board[0].size(); n++){
if (board[m]
== word[0]){
record[m]
= true;
level = 1;
scan(m, n, judge, level, word, record, board);
if (judge) return judge;
record[m]
= false;
}
}
}
return judge;
}
void scan(int i, int j, bool &judge,int &level, string &word,vector<vector<bool>> &record,vector<vector<char>> &board){
if (level== word.size()){
judge = true;
return;
}
if (i + 1 < board.size() && !record[i + 1][j] && board[i + 1][j] == word[level]){ //往右
record[i + 1][j] = true;
level++;
scan(i + 1, j, judge, level, word, record, board);
level--;
if (judge) return;
record[i + 1][j] = false;
}
if (i - 1 >= 0 && !record[i - 1][j] && board[i - 1][j] == word[level]){ //往左
record[i - 1][j] = true;
level++;
scan(i - 1, j, judge, level, word, record, board);
level--;
if (judge) return;
record[i - 1][j] = false;
}
if (j + 1 < board[0].size() && !record[i][j + 1] && board[i][j+1] == word[level]){ //往下
record[i][j+1] = true;
level++;
scan(i, j + 1, judge, level, word, record, board);
level--;
if (judge) return;
record[i][j+1] = false;
}
if (j - 1 >= 0 && !record[i][j - 1] && board[i][j-1] == word[level]){ //往上
record[i][j-1] = true;
level++;
scan(i , j-1, judge, level, word, record, board);
if (judge) return;
level--;
record[i][j-1] = false;
}
}
};
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.
思路: 思路,用一个矩阵记录被使用的单词,但是代码跑了44ms,还有优化的空间
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
bool judge = false;
int level = 0;
vector<vector<bool>> record(board.size(), vector<bool>(board[0].size(), false));
for (int m = 0; m <board.size(); m++){
for (int n = 0; n <board[0].size(); n++){
if (board[m]
== word[0]){
record[m]
= true;
level = 1;
scan(m, n, judge, level, word, record, board);
if (judge) return judge;
record[m]
= false;
}
}
}
return judge;
}
void scan(int i, int j, bool &judge,int &level, string &word,vector<vector<bool>> &record,vector<vector<char>> &board){
if (level== word.size()){
judge = true;
return;
}
if (i + 1 < board.size() && !record[i + 1][j] && board[i + 1][j] == word[level]){ //往右
record[i + 1][j] = true;
level++;
scan(i + 1, j, judge, level, word, record, board);
level--;
if (judge) return;
record[i + 1][j] = false;
}
if (i - 1 >= 0 && !record[i - 1][j] && board[i - 1][j] == word[level]){ //往左
record[i - 1][j] = true;
level++;
scan(i - 1, j, judge, level, word, record, board);
level--;
if (judge) return;
record[i - 1][j] = false;
}
if (j + 1 < board[0].size() && !record[i][j + 1] && board[i][j+1] == word[level]){ //往下
record[i][j+1] = true;
level++;
scan(i, j + 1, judge, level, word, record, board);
level--;
if (judge) return;
record[i][j+1] = false;
}
if (j - 1 >= 0 && !record[i][j - 1] && board[i][j-1] == word[level]){ //往上
record[i][j-1] = true;
level++;
scan(i , j-1, judge, level, word, record, board);
if (judge) return;
level--;
record[i][j-1] = false;
}
}
};
相关文章推荐
- AVL树的C语言实现
- hdu2570 迷瘴(贪心)
- 使用Visual Studio开发游戏——微软宣布与Unity、Unreal Engine和Cocos展开全新合作
- java中的serializable接口详解
- 关于二分查找, 关注细节
- LeetCode Anagrams
- Pie
- poj 2631 Roads in the North 【树的直径裸题】
- MFC技术内幕系列之(一)---MFC应用程序“生死因果”内幕
- LeetCode Anagrams
- RHEL7.1安装后进入X环境
- Binary Search Tree Iterator ***
- Ruby中编写类与模块的风格指南
- Javascript连接Access数据库完整实例
- Unreal、CryEngine、Gamebryo引擎介绍
- IE11 Windows7下F12 DOC资源管理器不能用Exception in window.onload: Error: An error has ocurredJSPlugin.3005
- JavaScript学习笔记——数组
- Red and Black
- Word Break
- MySQL 同步机制