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.

解题思路:

使用深度优先搜索的方法,从二维数组中的每个点出发,向上下左右进行延伸,根据判定条件及时判断;

解题步骤:

1、写一个主函数:

  (1)获得二维数组的行列大小;

  (2)遍历所有二维数组元素,对每个元素作为搜索起点,调用递归函数;

2、写一个递归函数,参数为:二维数组(引用)、待匹配的字符串(c字符串形式方便)、当前运动到的坐标值x和y、二维数组大小m和n;

  (1)终止判定条件:x和y无效、当前运动到的字符不是字符串下一个字符;

  (2)如果已经到达匹配字符串末尾,返回true

  (3)暂时将当前运动到的坐标字符置换为'\0',因为要做出标记,以免重复折返运动;

  (4)调用4个递归函数,分别运动到当前运动字符的上下左右四周,并判定;

  (5)如果4个递归函数都false,则换回被置换的'\0',返回false;

代码:

 class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
m = board.size();
n = board[].size();
for(int x = ; x < m; x++) {
for(int y = ; y < n; y++) {
if(isFound(board, word.c_str(), x, y))
return true;
}
}
return false;
} private:
int m;
int n;
bool isFound(vector<vector<char> > &board, const char* w, int x, int y) {
if(x < || y < || x >= m || y >= n || *w != board[x][y])
return false;
if(*(w + ) == '\0')
return true;
char t = board[x][y];
board[x][y] = '\0';
if(isFound(board, w + , x - , y) ||
isFound(board, w + , x + , y) ||
isFound(board, w + , x, y - ) ||
isFound(board, w + , x, y + ))
return true;
board[x][y]=t;
return false;
}
};

【Leetcode】【Medium】word search的更多相关文章

  1. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  6. 【LeetCode每天一题】Word Search(搜索单词)

    Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from le ...

  7. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

  9. 【LeetCode题意分析&解答】33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. CmdBuild

    cmdBuild官网地址:http://www.cmdbuild.org/it 下载.功能和安装说明:http://www.cmdbuild.org/en/download 扩展组件: shark-c ...

  2. 【转载】C++ IO库

    本篇随笔为转载,原贴地址:<C++ Primer>第8章 IO库 学习笔记. 1.IO类 #include <iostream> istream//从流中读取数据 ostrea ...

  3. NAT协议

    NAT服务器的设定   NAT的全名:Network Address Translation;即网络地址的转换: iptables指令就能够修改IP封包的表头数据,IP的目标地址,源地址都可以修改. ...

  4. UVa 490 - Rotating Sentences

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  5. 去掉DLL can move

    1.OptionalHeader.DllCharacteristics = wNewDllCharacteristics; 用CFF打开,如果存在DLL can move这个选项,去掉即可 2.Rel ...

  6. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. Android之ImageView 旋转

    方案一: (1)获取ImageView对应的图片,并将其转成Bitmap; (2)创建Matrix对象; (3)调用matrix.setRotate(     );设置旋转度数 (4)重新创建bitm ...

  8. Verilog之电平检测

    检测低电平为例 module detect_module ( CLK, RSTn, RX_Pin_In, H2L_Sig ); input CLK; input RSTn; input RX_Pin_ ...

  9. Appium客户端

    Appium版本:1.5.3 Xcode有两个版本:Xcode8.1   Xcode7.2.1 iOS10以下只能用Xcode7.2.1 iOS10及以上可以用Xcode8.1   1.Appium客 ...

  10. 分布式日志2 用redis的队列写日志

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...