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 ;
};

LeetCode_Word Search的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. 基于WebGL 的3D呈现A* Search Algorithm

    http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...

  5. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  6. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  7. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  8. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. Linux Top使用说明

    运行top后,按P键就按CPU排序,按M键就按内存排序 P – 以 CPU 占用率大小的顺序排列进程列表 M – 以内存占用率大小的顺序排列进程列表 在系统维护的过程中,随时可能有需要查看 CPU 使 ...

  2. [转]Ubuntu Linux 安装 .7z 解压和压缩文件

    原文网址:http://blog.csdn.net/zqlovlg/article/details/8033456 安装方法: sudo apt-get install p7zip-full 解压文件 ...

  3. 好用的QT连接

    QT属性控件项目https://github.com/lexxmark/QtnProperty比特币交易软件https://github.com/JulyIGHOR/QtBitcoinTrader导航 ...

  4. <php>统计目录数和文件数

    $dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...

  5. MyBatis魔法堂:Insert操作详解

    一.前言 数据库操作怎能少了INSERT操作呢?下面记录MyBatis关于INSERT操作的笔记,以便日后查阅. 二. insert元素 属性详解 其属性如下: parameterType:入参的全限 ...

  6. 利用dedecms给近三天(或当天)发布的文章显示红色日期或加上new字或new小图片

    1)红色日期 <br>[field:pubdate runphp='yes'] <br>$a="<font color=red>".strfti ...

  7. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  8. html5图片标签与属性

    标记:  标 记  说 明 <lmg> 图像 <Map> 图像映射 <Area> 图像映射中定义区域 <lmg>标记属性:  属 性  说 明 Src ...

  9. html游戏引擎,createJs框架

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! createJs网上的中文教程挺少的,以前UC有个Xcanvas的论坛有createJs的详细教程,但是随着XCanvas团队的解散,那个 ...

  10. 无法从带有索引像素格式的图像创建graphics对象(转)

    大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...