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. Android ExpandableListActivity的简单介绍及小例子

    Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表: 下面用一个例子来 ...

  2. POJ1159 Palindrome(数位DP)

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 58277   Accepted: 20221 Desc ...

  3. Hive 1、什么是Hive,Hive有什么用

    一.什么是Hive Hive是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据 ...

  4. poj 1742 Coins(dp之多重背包+多次优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  5. java.lang.NoClassDefFoundError 异常

    在项目实施过程中,当访问某一个功能时,出现异常为  java.lang.NoClassDefFoundError  com/xxx/yyy/Zzzz > ,检查发现这个类实际已经存在于应用服务器 ...

  6. App上线流程全攻略(续)-iOS8之后的改动与所遇日常错误

    随着iOS8的公布,iTunes Connect的界面也是发生了非常大的改变,App 上传到 Store上面的步骤也是发生了些改变.以下继续用图说话: /*********************** ...

  7. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  8. Ubuntu 10.04下安装Qt

    sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer qt-creator 其中: qt4-de ...

  9. android避免service被杀

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建@Override    public int onStartComma ...

  10. _js day12