题目

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;
}
};
假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30758751
)

[LeetCode] Word Search [37]的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  4. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  5. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

  6. [leetcode]Word Search @ Python

    原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...

  7. [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 ...

  8. leetcode Word Search 待解决?

    终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...

  9. [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 ...

随机推荐

  1. oracle系统包——dbms job用法(oracle定时任务)

    用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务. 一.dbms_job涉及到的知识点1.创建job:variable jobno number;dbms_job.su ...

  2. iOS中使用UIWebView与JS进行交互

    iOS中使用UIWebView与JS进行交互 前一段忙着面试和复习,这两天终于考完试了,下学期的实习也有了着落,把最近学的东西更新一下,首先是使用UIWebView与JS进行交互 在webView中我 ...

  3. 自由创造属于你的H5内容

    在这里,你可以自由创造属于你的H5内容  mark下 http://www.ih5.cn/

  4. 关于Python文档读取UTF-8编码文件问题

    近来接到一个小项目,读取目标文件中每一行url,并逐个请求url,拿到想要的数据. #-*- coding:utf-8 -*- class IpUrlManager(object): def __in ...

  5. Day1 初识Python

    (1)变量与赋值 name = "wanghuafeng" age = 29 print(name, age) a和b交换值 a = 3 b = 5 tmp = a a = b b ...

  6. django 序列化json问题

    from django.core import serializers @login_required def ajax_get_data(request): json_data = serializ ...

  7. C# 部分关键字

    关键字: virtual:  虚方法,本身可以被实例化,也可以在派生类中重写该方法: override:在派生类重写基类虚方法时声明,避免了C++中的潜在运行错误: abstract:声明为抽象类.抽 ...

  8. Could not launch process failed:security

    是因为  用了 企业的开发者账号   安装的时候需要  在 手机的设置中  找到 描述文件   然后点击信任这个对应的证书   才能使用这个由企业号发布的应用.

  9. json-lib 使用教程

    //关于java map与JSONObject类互相转换 Map<String,Object> map=new HashMap<String,Object>(); map.pu ...

  10. nutch Fetcer阶段详解

    job.setSpeculativeExecution(false); 抓网页阶段,不允许同一个任务运行多次,否则,网页就抓重了 为了充分利用闲置资源,加快map 和 reduce 的执行,于是有Sp ...