题目

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 =

  1. [
  2. ['A','B','C','E'],
  3. ['S','F','C','S'],
  4. ['A','D','E','E']
  5. ]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

解答

其实就是个DFS,太水了以至于一遍就AC了。。。

下面是AC的代码:

  1. class Solution {
  2. public:
  3. int **flag;
  4. bool search(vector<vector<char>>& board, string word, int row, int col){
  5. if(row < 0 || row >= board.size() || col < 0 || col >= board[0].size()){
  6. return false;
  7. }
  8. if(word[0] == board[row][col] && flag[row][col] == 0){
  9. if(word.size() == 1){
  10. return true;
  11. }
  12. else{
  13. int length = word.size();
  14. flag[row][col] = 1;
  15. int ret = search(board, word.substr(1, length - 1), row - 1, col)
  16. || search(board, word.substr(1, length - 1), row, col + 1)
  17. || search(board, word.substr(1, length - 1), row + 1, col)
  18. || search(board, word.substr(1, length - 1), row, col - 1);
  19. if(ret == true){
  20. return true;
  21. }
  22. else{
  23. flag[row][col] = 0;
  24. return false;
  25. }
  26. }
  27. }
  28. else{
  29. return false;
  30. }
  31. }
  32. bool exist(vector<vector<char>>& board, string word) {
  33. int row = board.size();
  34. int col = board[0].size();
  35. flag = new int*[row];
  36. for(int i = 0; i < row; i++){
  37. flag[i] = new int[col];
  38. for(int j = 0; j < col; j++){
  39. flag[i][j] = 0;
  40. }
  41. }
  42. for(int i = 0; i < row; i++){
  43. for(int j = 0; j < col; j++){
  44. if(search(board, word, i, j) == true){
  45. return true;
  46. }
  47. }
  48. }
  49. return false;
  50. }
  51. };

117

LeetCode OJ 79. Word Search的更多相关文章

  1. 【LeetCode】79. Word Search

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

  2. 【一天一道LeetCode】#79. Word Search

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. LeetCode OJ: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】79. Word Search 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  5. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  6. [LeetCode] 79. Word Search 词语搜索

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

  7. [LeetCode] 79. 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 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  9. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

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

随机推荐

  1. SpringMVC 注解详解

    SpringMVC常用注解说明 @Bean, @Configuration表示基于Java配置的类@Bean除了配置在@Configuration,也可以在@Component定义,此时没有特殊意义, ...

  2. C#连接数据库最基本操作之sql语句 DML

    C#连接数据库最基本操作之sql语句 DML //1 连接字符串 string connectionString = "server=127.0.0.1;integrated securit ...

  3. vue事件深入

    事件对象: @click="show($event)" ev.clientX---接收 事件冒泡: 阻止冒泡: 1. ev.cancelBubble=true; 2.@click. ...

  4. Linux常用指令之一

    1.基础命令 ls --查看当前目录下的文件     cd --切换目录或者直接回到home目录     cd - --切换最近使用的两次目录     cd .. --切换到上一级目录     pwd ...

  5. 思考-Status management and validation(状态管理与验证)

    结合自己的项目,有这么一个模块,这个模块用来添加一个停车场,注册信息又分:基本信息,管理设置,管理员设置3部分组成,每部分都有input=text的输入框,点击保存按钮需要验证各个部分的输入框是否有合 ...

  6. FastDFS+Nginx+fastdfs-nginx-module集群搭建

    一.实验环境说明 操作系统: Centos 6.6 x64 FastDFS 相关版本: fastdfs-5.05 fastdfs-nginx-module-v1.16 libfastcommon-v1 ...

  7. Windows向虚拟机Linux传输文件方法

    在Windows中装了个centOS,进行文件操作时,把mv写成了rm,然后就悲剧了.. 赶紧从网上找来文件的具体内容,然后由Windows向Linux挂载共享文件夹. 具体做法: 在Windows中 ...

  8. java的list遍历

    for(String str : list) {//增强for循环,其内部实质上还是调用了迭代器遍历方式,这种循环方式还有其他限制,不建议使用. System.out.println(str); } ...

  9. return的一种用法:如果当前判断为true则跳出这个方法。

    package rom; import java.lang.*; /* * return的一种用法:如果当前判断为true则跳出这个方法. */ public class Xamle_5 { stat ...

  10. 《算法》BEYOND 部分程序 part 3

    ▶ 书中第六章部分程序,加上自己补充的代码,包括 Graham 扫描生成凸包,计算最远点对 ● Graham 扫描生成凸包 package package01; import java.util.Ar ...