题目描述

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.

Example:

board =

[

['A','B','C','E'],

['S','F','C','S'],

['A','D','E','E']

]

Given word = "ABCCED", return true.

Given word = "SEE", return true.

Given word = "ABCB", return false.

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/word-search

代码实现

class Solution {
public:
bool existCore(vector<vector<char>>& board, string &word,int & Wordpos,int rows,int cols,int row,int col,vector<vector<int> > & visited){
if(Wordpos>=word.size())
return true;
if(col>=cols||row>=rows||col<0||row<0){
return false;
}
bool flag=false;
if(board[row][col]==word[Wordpos]&& visited[row][col]!=1){
++Wordpos;
visited[row][col]=1;
flag=existCore(board,word,Wordpos,rows,cols,row+1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col+1,visited)||existCore(board,word,Wordpos,rows,cols,row-1,col,visited)||existCore(board,word,Wordpos,rows,cols,row,col-1,visited);
if(!flag)
{
--Wordpos;
visited[row][col]=0;
}
}
return flag;
}
bool exist(vector<vector<char>>& board, string word) {
bool result=false;
if(board.size()==0||board[0].size()==0){
return false;
}
int rows=board.size();
int cols=board[0].size();
vector<vector<int> > visited;
for(int i=0;i<rows;++i){
vector<int> temp;
for(int j=0;j<cols;++j){
temp.push_back(0);
}
visited.push_back(temp);
}
int Wordpos=0;
for(int i=0;i<rows;++i){
for(int j=0;j<cols;++j)
{
if(existCore(board,word,Wordpos,rows,cols,i,j,visited))
return true;
}
}
return false;
}
};

总结

思路就是回溯法。中间纠结了一下,visited的数组是用二维数组还是用vector。后来发现如果用二维数组,传指针的地方较为麻烦,而且不能像vector一样直接visited[row][col]。

Leetcode79 Word Search的更多相关文章

  1. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

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

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

  4. Leetcode: word search

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

  5. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  6. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  7. Java for LeetCode 212 Word Search II

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

  8. 51. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

随机推荐

  1. appium--解决中文输入不了的问题

    配置 from appium import webdriver desired_caps={} desired_caps['platformName']='Android' #模拟器 desired_ ...

  2. 10.24考试题解qwq

    考点难度都很合适的一套题目,大概在day1到day2之前 T1 猴猴最喜欢在树上玩耍,一天猴猴又跳上了一棵树,这棵树有N个苹果,每个苹果有一个编号,分别为0~N-1,它们之间由N-1个树枝相连,猴猴可 ...

  3. 【转】hibernate对象三种状态

    hibernate里对象有三种状态: 1,Transient 瞬时 :对象刚new出来,还没设id,设了其他值. 2,Persistent 持久:调用了save().saveOrUpdate(),就变 ...

  4. Linux性能优化实战学习笔记:第二十七讲

    一.案例环境描述 1.环境准备 2CPU,4GB内存 预先安装docker sysstat工具 2.温馨提示 案例中 Python 应用的核心逻辑比较简单,你可能一眼就能看出问题,但实际生产环境中的源 ...

  5. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  6. WPF CoboxItem控件使用SelectedItem去调System.Windows.Controls.ComboBoxItem: 前缀方法

    textComBox.SelectedItem as ComboBoxItem).Content textConbox: 控件Combobox 的Name 在Combobox控件SelectionCh ...

  7. 基于Kafka的实时计算引擎如何选择?Flink or Spark?

    1.前言 目前实时计算的业务场景越来越多,实时计算引擎技术及生态也越来越成熟.以Flink和Spark为首的实时计算引擎,成为实时计算场景的重点考虑对象.那么,今天就来聊一聊基于Kafka的实时计算引 ...

  8. 全局安装npm包报错没有权限

    背景:npm i npm-check -g 时报错没有权限 Error: EACCES: permission denied, access '/usr/local/lib/node_modules' ...

  9. 哪个版本的gcc才支持c11

    而知,低版本的gcc不支持c11. (而我此处的eglibc 2.17,和那人的glibc-2.16.0,都是需要支持c11的gcc的) 所以此处想要去搞清楚,什么版本的,哪个版本的,gcc,才支持c ...

  10. 如何防止短信API接口遍历

    短信API接口在web中得到越来越多的应用,如用户注册,登录,密码重置等业务模块都会使用手机验证码进行身份验证.一般情况下,我们会采用这样的安全策略,将短信发送频率限制在正常的业务流控范围内,比如,一 ...