212. 单词搜索 II

这leetcode的评判机绝对有问题!!同样的代码提交,有时却超时!害得我至少浪费两个小时来寻找更优的答案= =,其实第一次写完的代码就可以过了,靠!!!第207位做出来的

趁热,我把79. 单词搜索也做了一下

与79题完全,无非是从一个word变成了多个word,我们多次调用79题的代码即可

这样的话,我们能过掉除了最后一组数组外的其它所有数据。最后一组数据超时。

那么我们就得考虑如何优化了,优化方向也就是题目中提示所说的:

  1. 提前停止回溯。这个在79题的代码中已经做到了,找到了后直接结束搜索就好了,不必再继续回溯。
  2. 如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。我举个例子:如果abc不存在于网格中,那么以abc为子串的所有word,肯定也不存在于网格中。我们将words排序,凡是不存在于网格中的word,将其标记起来,即为wordNotExist,后面再遍历word时,先看其包不包含wordNotExist,如果包含,则不用搜了。代码如下:
boolean notExistFlag = false;
for (int j = 1; j < word.length(); j++) {
if (notExistWords.containsKey(word.substring(0, j + 1))) {
notExistFlag = true;
break;
}
}
if (notExistFlag) continue;

全部代码

class Solution {
static int[][] d = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
static Map<String, Boolean> notExistWords = new HashMap<>(); public List<String> findWords(char[][] board, String[] words) {
List<String> ans = new ArrayList<>();
Arrays.sort(words);
notExistWords.clear(); for (int i = 0; i < words.length; i++) {
String word = words[i];
// 去重
if (i > 0 && word.equals(words[i - 1])) continue; // 优化
boolean notExistFlag = false;
for (int j = 1; j < word.length(); j++) {
if (notExistWords.containsKey(word.substring(0, j + 1))) {
notExistFlag = true;
break;
}
}
if (notExistFlag) continue; if (exist(board, word)) {
ans.add(word);
} else {
notExistWords.put(word, false);
}
} return ans;
} public boolean exist(char[][] board, String word) {
int m = board.length;
if (m == 0) return false;
int n = board[0].length;
if (n == 0) return false;
if (word.equals("")) return true;
boolean[][] f = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (word.charAt(0) == board[i][j]) {
f[i][j] = true;
boolean flag = dfs(i, j, 1, board, word, m, n, f);
if (flag) return true;
f[i][j] = false;
}
}
}
return false;
} private boolean dfs(int i, int j, int k, char[][] board, String word, int m, int n, boolean[][] f) {
if (k == word.length()) {
return true;
}
for (int l = 0; l < 4; l++) {
if (i + d[l][0] < m && j + d[l][1] < n && i + d[l][0] >= 0 && j + d[l][1] >= 0 && board[i + d[l][0]][j + d[l][1]] == word.charAt(k) && !f[i + d[l][0]][j + d[l][1]]) {
f[i + d[l][0]][j + d[l][1]] = true;
boolean flag = dfs(i + d[l][0], j + d[l][1], k + 1, board, word, m, n, f);
if (flag) return true;
f[i + d[l][0]][j + d[l][1]] = false;
}
}
return false;
}
}

[leetcode] 212. 单词搜索 II(Java)的更多相关文章

  1. Java实现 LeetCode 212 单词搜索 II(二)

    212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...

  2. [LeetCode] 212. 单词搜索 II

    题目链接:https://leetcode-cn.com/problems/word-search-ii/ 题目描述: 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在 ...

  3. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...

  4. Java实现 LeetCode 212 单词搜索 II

    public class Find2 { public int[] dx={1,-1,0,0}; public int[] dy={0,0,1,-1}; class Trie{ Trie[] trie ...

  5. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  6. 212. 单词搜索 II

    Q: 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻" ...

  7. [LeetCode] 212. Word Search II 词语搜索 II

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

  8. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

  9. Java实现 LeetCode 79 单词搜索

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

随机推荐

  1. 【并发编程】线程池是否需要手动关闭吗?以Hutool中的线程池为例

    Hutool工具包中使用线程池的API是: ThreadUtil.execute() /** * 直接在公共线程池中执行线程 * * @param runnable 可运行对象 */ public s ...

  2. Hook android系统调用的实践

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/71037182 一.环境条件 Ubuntukylin 14.04.5 x64bit ...

  3. Winrar漏洞复现(CVE-2018-20250)

    本文讲的是Winrar漏洞利用脚本的使用方法,至于Winrar漏洞的原理,请移步--> Winrar目录穿越漏洞复现 本次利用脚本出处--> https://github.com/back ...

  4. Windows PE导出表编程2(重组导出表函数地址)

    本次要做的尝试是通过修改导出表的函数地址,实现程序功能的更改,实现这个最大的限制就是堆栈平衡问题. 先写一个DLL和EXE为了测试. DLL代码如下: 这样的话有两个导出函数(我们假设是一个密码验证之 ...

  5. markdown 实现代码折叠效果

    展开:我是一个挑山工,仙人跳 #include int main() { printf("挑山工,快乐加倍"); } 展开:我是一个挑山工,仙人跳 #include int mai ...

  6. 论文解读丨基于局部特征保留的图卷积神经网络架构(LPD-GCN)

    摘要:本文提出一种基于局部特征保留的图卷积网络架构,与最新的对比算法相比,该方法在多个数据集上的图分类性能得到大幅度提升,泛化性能也得到了改善. 本文分享自华为云社区<论文解读:基于局部特征保留 ...

  7. Portswigger web security academy:OAth authentication vulnerable

    Portswigger web security academy:OAth authentication vulnerable 目录 Portswigger web security academy: ...

  8. Docker为PHP安装gd扩展

    安装扩展库的通常命令 docker-php-ext-install 扩展库名 安装gd库需要特殊照顾,步骤如下 //进入PHP容器 //更新软件源 apt update //安装各种库 apt ins ...

  9. 【实用小技巧】spring springmvc集成shiro时报 No bean named 'shiroFilter' available

    查了网上的,很多情况,不同的解决办法,总归一点就是配置文件加载的问题. 先看下配置文件中的配置 web.xml中的主要配置(这是修改后不在报错的:仅仅修改了一个位置:[classpath:spring ...

  10. C++ string的size()和length()函数没有区别

    C++标准库中的string中两者的源代码如下:      size_type   __CLR_OR_THIS_CALL   length()   const     { //   return   ...