Leetcode 212.单词搜索II
单词搜索II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
示例:
输入:
words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
输出: ["eat","oath"]
说明:
你可以假设所有输入都由小写字母 a-z 组成。
提示:
- 你需要优化回溯算法以通过更大数据量的测试。你能否早点停止回溯?
- 如果当前单词不存在于所有单词的前缀中,则可以立即停止回溯。什么样的数据结构可以有效地执行这样的操作?散列表是否可行?为什么? 前缀树如何?如果你想学习如何实现一个基本的前缀树,请先查看这个问题: 实现Trie(前缀树)。
方法:前缀树+深度优先搜索。
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; public class Solution {
private TrieNode root = new TrieNode();
private int[] ro = {-1, 1, 0, 0};
private int[] co = {0, 0, -1, 1};
private void find(char[][] board, boolean[][] visited, int row, int col, TrieNode node, Set<String> founded) {
visited[row][col] = true;
TrieNode current = node.nexts[board[row][col]-'a'];
if (current.word != null) founded.add(current.word);
for(int i=0; i<4; i++) {
int nr = row + ro[i];
int nc = col + co[i];
if (nr < 0 || nr >= board.length || nc < 0 || nc >= board[nr].length || visited[nr][nc]) continue;
TrieNode next = current.nexts[board[nr][nc]-'a'];
if (next != null) find(board, visited, nr, nc, current, founded);
}
visited[row][col] = false;
}
public List<String> findWords(char[][] board, String[] words) {
Set<String> founded = new HashSet<>();
for(int i=0; i<words.length; i++) {
char[] wa = words[i].toCharArray();
TrieNode node = root;
for(int j=0; j<wa.length; j++) node = node.append(wa[j]);
node.word = words[i];
}
boolean[][] visited = new boolean[board.length][board[0].length];
for(int i=0; i<board.length; i++) {
for(int j=0; j<board[i].length; j++) {
if (root.nexts[board[i][j]-'a'] != null) find(board, visited, i, j, root, founded);
}
}
List<String> results = new ArrayList<>();
results.addAll(founded);
return results;
}
}
class TrieNode {
String word;
TrieNode[] nexts = new TrieNode[26];
TrieNode append(char ch) {
if (nexts[ch-'a'] != null) return nexts[ch-'a'];
nexts[ch-'a'] = new TrieNode();
return nexts[ch-'a'];
}
}
] = new TrieNode();
return nexts[ch-'a']; }}
Leetcode 212.单词搜索II的更多相关文章
- [leetcode] 212. 单词搜索 II(Java)
212. 单词搜索 II 这leetcode的评判机绝对有问题!!同样的代码提交,有时却超时!害得我至少浪费两个小时来寻找更优的答案= =,其实第一次写完的代码就可以过了,靠!!!第207位做出来的 ...
- [LeetCode] 212. 单词搜索 II
题目链接:https://leetcode-cn.com/problems/word-search-ii/ 题目描述: 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在 ...
- Java实现 LeetCode 212 单词搜索 II(二)
212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...
- 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 ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- 212. 单词搜索 II
Q: 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻" ...
- [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 ...
- Leetcode 79.单词搜索
单词搜索 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单 ...
- Java实现 LeetCode 140 单词拆分 II(二)
140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...
随机推荐
- F - Function
Bryce1010模板 先找到数组A中的循环节,再找到数组B中的循环节,如果B中的循环节是A中循环节的循环因子,说明可以配对,结果累积起来. #include<bits/stdc++.h> ...
- BitCoin工作原理
1.加密货币 公共账本-信任+加密算法=加密货币 BitCoin是第一个被是实现出来的加密货币. 首先理解比特币是什么,在考虑要不要买入?(人人都想一夜暴富,美哉) 2.发送.接收.创造比特币的时候电 ...
- 题解报告:hdu 1062 Text Reverse
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 Problem Description Ignatius likes to write word ...
- JEECMS9.3集成dubbo操作记录
需求描述: 门户及其他应用系统需要查询JEECMS9.3中发布的栏目及数据,而其他系统都是基于dubbo开发的,因此想要将JEECMS9.3中集成dubbo并对外提供内容管理服务. 需求实现: 1.添 ...
- http缓存之lastModified和etag
1.cache-control 访问资源 首次访问页面时间:2018.2.1 9:56 (当前时间=GMT时间+8h) 缓存时长max-age:1 day Expire缓存失效时间:2018.2. ...
- OSW
OSWatcher 工具 下载文档 :Metalink Doc ID 301137.1 Oswatcher 主要用于监控主机资源,如CPU,内存,网络以及私有网络等.其中私有网络需要单独配置. 需要说 ...
- ORA-00020: maximum number of processes (300) exceeded
SQL> select count(*) from v$session; COUNT(*)---------- 98 SQL> select count(*) from v$process ...
- C# 代码笔记_tuple元组
tuple元组: 赋值 List<Tuple<string, int>> cc = new List<Tuple<string, int>>() { n ...
- IPython notebook快捷键(Jupyter notebook)
转自“https://blog.csdn.net/eswai/article/details/53642802” 本文整理了神器IPython Notebook(或Jupyter Notebook)的 ...
- redis 在windows 集群
前言:为什么自己要花时间写一篇redis集群文章,网上众多的文章大都是思路正确,但是细节不足,这里写一篇文章记录自己部署时候遇到的问题,当下次再部署的时候避免跳入重复的坑. 上篇文章(http://w ...