原题链接在这里:https://leetcode.com/problems/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 =

[['A','B','C','E'],['S','F','C','S'],['A','D','E','E']
]

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

题解:

类似N-Queens. dfs, 终止条件若能走到index == word.length(), 返回true.

若是做不到就是中间出现了越界或者不等,返回false.

用之前要把used 对应位置改为true, 用完后记得把used 对应位置改为false.

最后返回res.

Time Complexity: O(m^2 * n^2).  对于board每一个点search都是一次dfs, dfs用了O(V+E), 矩阵表示时就是O(m*n). 并且board一共有m*n个点.

Space: O(m*n). 因为用了used存储。这里m=board.length, n = board[0].length.

AC Java:

 1 public class Solution {
 2     public boolean exist(char[][] board, String word) {
 3     if(word==null || word.length()==0)
 4         return true;
 5     if(board==null || board.length==0 || board[0].length==0)
 6         return false;
 7     boolean[][] used = new boolean[board.length][board[0].length];
 8     for(int i=0;i<board.length;i++)
 9     {
10         for(int j=0;j<board[0].length;j++)
11         {
12             if(search(board,word,0,i,j,used))
13                 return true;
14         }
15     }
16     return false;
17 }
18 private boolean search(char[][] board, String word, int index, int i, int j, boolean[][] used)
19 {
20     if(index == word.length())
21         return true;
22     if(i<0 || j<0 || i>=board.length || j>=board[0].length || used[i][j] || board[i][j]!=word.charAt(index))
23         return false;
24     used[i][j] = true;
25     boolean res = search(board,word,index+1,i-1,j,used)
26                 || search(board,word,index+1,i+1,j,used)
27                 || search(board,word,index+1,i,j-1,used)
28                 || search(board,word,index+1,i,j+1,used);
29     used[i][j] = false;
30     return res;
31     }
32 }

跟上Word Search II

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4944270.html

LeetCode 79. Word Search相关推荐

  1. 【DFS + Backtracking】LeetCode 79. Word Search

    LeetCode 79. Word Search Solution1:我的答案 DFS + Backtracking class Solution { public:bool exist(vector ...

  2. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  3. leetcode 79. Word Search | 79. 单词搜索(回溯+DFS)

    题目 https://leetcode.com/problems/word-search/ 题解 回溯 + DFS,回溯是有后效性的,所以不能转化为 DP class Solution {int M; ...

  4. 【DFS + Backtracking】LeetCode 212. Word Search II

    LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...

  5. 79. Word Search

    leetcod79题在矩阵中寻找某个单词 """ 79. Word Search MediumShare Given a 2D board and a word, fin ...

  6. leetcode 212. Word Search II | 212. 单词搜索 II(Trie,回溯,DFS)

    题目 https://leetcode.com/problems/word-search-ii/ 题解 基于前缀树实现,如果把 Trie 也看做一个特殊的图的话,就是 将两个图同时进行 dfs,就像判 ...

  7. 79. Word Search 单词搜索

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

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

  9. 【CODE】Unique Paths Word Search (DFS dp 字典树)

    目录 62. Unique Paths 63. Unique Paths II 980. Unique Paths III 79. Word Search 212. Word Search II 字典 ...

最新文章

  1. Spring Boot + EasyExcel 导入导出,好用到爆,可以扔掉 POI 了!
  2. NYOJ5056_黑色帽子(水题)
  3. ansible(基础)
  4. Spring源码分析前篇
  5. 检查MySQL主从数据一致性
  6. ubuntu 18.04 显卡驱动
  7. 多通路fpga 通信_多核DSP和FPGA之间的高速SRIO通信
  8. python管道pipe_Python multiprocessing模块中的Pipe管道
  9. 分享活动报名收费的微信小程序制作功能介绍_瑜伽健身房培训报名小程序开发介绍
  10. PPT设置自动保存时间 mac_PPT又崩溃了?教你如何恢复文档
  11. opencv RGB三通道分离
  12. 【SWT】创建自己的SWT组件
  13. 小米电脑桌面没见计算机怎么办,手机屏幕太小?一分钟教会你小米手机投屏电脑方法,低调收藏!...
  14. math_@多元函数求导@全微分@偏导数@复合偏导
  15. 用JSP/Servlet应用开发一个简单的考试报名系统
  16. 穿过黑暗的夜,才懂黎明的晨
  17. 易语言lsp劫持_易语言如何制作LSP修复工具,用来解决网截模块问题.
  18. ifconfig 下面的一些字段(errors, dropped, overruns)
  19. 情人节,送女友一桶代码可否?
  20. 计算机网络第一二章多选题,计算机网络概论第一章练习题

热门文章

  1. 暑期项目经验(九) -- request session application
  2. 面向对象编程学习5月7日-5月23日 网络直播yii-外企使用最多的PHP框架
  3. hadoop+hive-0.10.0完全分布式安装方法
  4. sockets C#
  5. ListString 和 ArrayListString的区别
  6. 15个Java多线程面试题
  7. 我看objective-C --不要把objC当做c/c++的超集
  8. 《深入理解Linux内核》笔记5:内存管理
  9. 双绞线施工质量的检测方法
  10. 机器学习kaggle竞赛实战-泰坦尼克号