[LeetCode] Word Search [37]
题目
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.
解题思路
给一个二维字符数组,给一个字符串,问该二维数组是否包括该字符串。比方一个二维数组[ ABCE SFCS ADEE ]和字符串"ABCCED",这个就包括。解决问题,基本的关键是怎么解决在二维数组中查找方向,怎样来标识哪些是走过的。
代码实现
class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        int m = board.size();
        if(m<=0) return false;
        int n = board[0].size();
        for(int i=0; i<m; ++i)
            for(int j=0; j<n; ++j){
                if(helper(0, word, i, j, board))
                    return true;
            }
        return false;
    }
    bool helper(int k, const string &word, int i, int j, vector<vector<char> > &board){
        if(k==word.size()-1 && board[i][j]==word[k])
            return true;
        if(board[i][j] != word[k])
            return false;
        char temp = board[i][j];
        // 走过的地方使用 '.'  来表示
        board[i][j] = '.';
        bool b1=false, b2=false, b3=false, b4=false;
        // board[i][j]的上面
        if(i>0 && board[i-1][j]!='.')
            b1 = helper(k+1, word, i-1, j, board);
        // board[i][j]的以下
        if(!b1 && i<board.size()-1 && board[i+1][j] != '.')
            b2 = helper(k+1, word, i+1, j, board);
        // board[i][j]的左面
        if(!b1 && !b2 && j>0 && board[i][j-1] != '.')
            b3 = helper(k+1, word, i, j-1, board);
        // board[i][j]的右面
        if(!b1 && !b2 && !b3 && j<board[0].size()-1 && board[i][j+1]!='.')
            b4 = helper(k+1, word, i, j+1, board);
        board[i][j] = temp;
        return b1 || b2 || b3 || b4;
    }
};
另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Word Search [37]的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
		
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
 - [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 ...
 - Leetcode: word search
		
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
 - LeetCode: Word Search  解题报告
		
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
 - LeetCode() Word Search II
		
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
 - [leetcode]Word Search @ Python
		
原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...
 - [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 ...
 - leetcode Word Search 待解决?
		
终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...
 - [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 ...
 
随机推荐
- (转)一步一步学习PHP(5)——类和对象
			
OO的强大我不想再多说,如果你不认同OO,那么当你放眼当前流行的语言,有哪个又不支持OO的,也许这个很有说服力了吧. 在这一节中,我们就来看看在PHP中如何创建一个类和对象. 1. 创建类 在PHP中 ...
 - 转:常用的HTML标签和属性解释
			
基本结构标签: <HTML>,表示该文件为HTML文件 <HEAD>,包含文件的标题,使用的脚本,样式定义等 <TITLE>---</TITLE>,包含 ...
 - 集成支付宝后出现LaunchServices: ERROR: There is no registered handler for URL scheme alipay
			
原因如下: There's no problem with your implementation. All those warnings mean is the apps which each UR ...
 - 你好,C++(40)7.1  一切指针都是纸老虎:彻底理解指针
			
第7章 C++世界的奇人异事 在武侠小说中,初入武林的毛头小子总是要遇到几位奇人,发生几件异事,经过高人的指点,经历一番磨炼,方能武功精进,从新手成长为高手.在C++世界,同样有诸多的奇人异事.在C+ ...
 - JavaScript错误处理
			
JavaScript 错误 - Throw.Try 和 Catch JavaScript 测试和捕捉 try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代 ...
 - HTML5 canvas生成图片马赛克特效插件
			
HTML5 canvas生成图片马赛克特效插件 简要教程 这是一款使用html5 canvas来将图片制作成马赛克效果的js插件.该插件的灵感来自于美国肖像画家Chuck Close.已经有人使用这个 ...
 - windows8.1 下搭建配置apache+php+mysql
			
软件版本: apache:Apache 2.4.10 Win64 http://www.apachelounge.com/download/VC11/binaries/httpd-2.4.10- ...
 - AngularJs 【使用】 -- ng-repart 排序使用
			
1.单字段 ng-repeat="item in dataList | orderBy:'field' " 2.多字段 ng-repeat="item in dataLi ...
 - SqlServer将数据库中的表复制到另一个数据库
			
前述: 文章来自百度经验 操作: 在使用SqlServer的过程中,我们可能需要将表从一个数据库复制到另一个数据库中,今天,为大家介绍这种操作的具体方法及步骤. 复制表结构 1 首先,打开并连接Sql ...
 - The executable was signed with invalid entitlements.
			
如图,出现这个的原因是 配置文件(provisioning profile)和 app 授权文件中的 entitlements(授权) 不匹配 具体应该从 配置文件 和证书的对应 问 ...