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. 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...

  2. 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

    原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...

  3. bzoj 1193

    http://www.lydsy.com/JudgeOnline/problem.php?id=1193 大范围贪心,小范围宽搜. 膜拜大神 http://blog.csdn.net/u0129155 ...

  4. debug openStack

    ERROR neutron.agent.l3.agent [-] An interface driver must be specified No valid host was found. Ther ...

  5. java对象在hibernate持久层的状态

    站在持久化层的角度,一个java对象在它的生命周期中,可处于以下4个状态之一: 临时状态(transient):刚刚用new语句创建,还没有被持久化,并且不处于Session的缓存中. 持久化状态(p ...

  6. c指针点滴三(指针运算)

    #include <stdio.h> #include <stdlib.h> void main3() { ; int *p = &num; p++;//不可预测的值 ...

  7. python RabbitMQ队列使用(入门篇)

    ---恢复内容开始--- python RabbitMQ队列使用 关于python的queue介绍 关于python的队列,内置的有两种,一种是线程queue,另一种是进程queue,但是这两种que ...

  8. HTML5新增的一些属性和功能之六——拖拽事件

    拖放事件的前提是分为源对象和目标对象,你鼠标拖着的是源对象,你要放置的位置是目标对象,区分这两个对象是因为HTML5的拖放事件对两者是不同的. 被拖动的源对象可以触发的事件: 1).ondragsta ...

  9. read(),write() 读/写文件

    read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...

  10. CSS transform(变形)和transform-origin(变形原点)

    transform(变形)和transform-origin(变形原点)的说明: 目前这两个属性得到了除去ie以外各个主流浏览器webkit,firefox,opera的支持,属性名分别为 -webk ...