Word Search

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.

 
 
 class Solution {
public: int n,n1,n2; bool exist(vector<vector<char> > &board, string word) { n1=board.size();
n2=board[].size();
n=word.length(); bool flag=false; //vector<vector<bool> > visited(n1,vector<bool>(n2,false)); bool **visited=new bool*[n1];
for(int i=;i<n1;i++)
{
visited[i]=new bool[n2];
for(int j=;j<n2;j++)
{
visited[i][j]=false;
}
} for(int i=;i<n1;i++)
{
for(int j=;j<n2;j++)
{
if(board[i][j]==word[])
{
//注意visited采用引用传值
flag=flag||dfs(board,word,i,j,visited);
if(flag) return true;
}
}
} for(int i=;i<n1;i++) delete[] visited[i]; return false;
} bool dfs(vector<vector<char> > &board,string &word,int i,int j,bool** &visited,int index=)
{ if(board[i][j]!=word[index]||visited[i][j]) return false;
if(index==n-) return true; visited[i][j]=true; bool flag1=i+<n1&&dfs(board,word,i+,j,visited,index+);
bool flag2=j+<n2&&dfs(board,word,i,j+,visited,index+);
bool flag3=j->=&&dfs(board,word,i,j-,visited,index+);
bool flag4=i->=&&dfs(board,word,i-,j,visited,index+); bool result=flag1||flag2||flag3||flag4; //由于是引用传值,所以没有找到的目标串时要把visited复原
//if(result==false) visited[i][j]=false;
visited[i][j]=result;
return result;
}
};
 
 
 

【leetcode】Word Search的更多相关文章

  1. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  2. 【leetcode】Word Search II(hard)★

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  3. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  4. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【Leetcode】【Medium】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 Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  7. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. 【leetcode】1268. Search Suggestions System

    题目如下: Given an array of strings products and a string searchWord. We want to design a system that su ...

  9. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

随机推荐

  1. OC-改错题

    1,类方法中不能访问成员变量 2,id后不能加*(因为id相当于NSObject *) 3,id类型的变量不能用点语法 4,类对象只能调用类方法,不能调用对象方法 .description #impo ...

  2. http的六种请求方法

    1.get: GET可以说是最常见的了,它本质就是发送一个请求来取得服务器上的某一资源.资源通过一组HTTP头和呈现数据(如HTML文本,或者图片或者视频等)返回给客户端.GET请求中,永远不会包含呈 ...

  3. cookie和session的对比

    1.存放的位置     cookie存在客户端的临时文件夹     session:存在服务器的内存中,一个session域对象为一个用户浏览器服务. 2.安全性   cookie是以明文方式存放在客 ...

  4. 在CentOS上搭建apache和PHP服务器环境(转)

    1.您也可以使用一键自动部署环境的工具,请参见网友开发的这个工具 http://www.centos.bz/2013/08/ezhttp-tutorial/ 2. 安装: wget -c http:/ ...

  5. 对AccessViolationException的一些总结

    引言 开发Winform程序时,应用程序出现了异常,整个应用程序崩溃自动退出了.在断点调试后,发现异常是AccessViolationException.所以对周围的语句加上了异常的处理机制.但是接下 ...

  6. JavaScript 五种(构造方式)继承

    一.对象冒充 function Parent(username){ this.username = username; this.hello = function(){ alert(this.user ...

  7. BufferedReader readLine()方法

    控制台输入字符串之后回车,后台接收传来的字符串,代码如下: import java.io.BufferedReader; import java.io.IOException; import java ...

  8. jQuery回调、递延对象总结(上篇)—— jQuery.Callbacks

    前言: 作为参数传递给另一个函数执行的函数我们称为回调函数,那么该回调又是否是异步的呢,何谓异步,如:作为事件处理器,或作为参数传递给 (setTimeout,setInterval)这样的异步函数, ...

  9. 在hexo静态博客中利用d3-cloud来展现标签云

    效果: http://lucyhao.com/tags/ hexo自带的tag cloud的标签展现不太美观,想能够展现出“云”效果的标签.在网上找到了d3-cloud这个项目,github地址:ht ...

  10. 开源一个动态解析protobuf的工具

    好久没写博客了,主要是这一年技术没啥长进都打杂了,还有就是生活琐事越来越多,人也越来越懒了…… 之前项目中用到了Protobuf,然后测试发现这玩意不好测,总不能每次定个协议或者改下都要编译Java代 ...