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 must 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 in a word.
For example,
Given words = ["oath","pea","eat","rain"] and board =
[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
Return ["eat","oath"].
解题思路:
本题承接自Java for LeetCode 079 Word Search 但是用dfs会TLE,真正的做法是将word放进Trie里面,然后遍历trie里的每个点,看是否能匹配到trie里的元素,JAVA实现如下:
public class Solution {
public List<String> findWords(char[][] board, String[] words) {
HashSet<String> list=new HashSet();
Trie trie = new Trie();
for (String word : words)
trie.insert(word);
boolean[][] visited=new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
dfs(list,board, visited, "", i, j, trie);
return new ArrayList(list);
}
public void dfs(Set<String> list,char[][] board, boolean[][] visited, String str, int x, int y, Trie trie) {
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length)
return;
if (visited[x][y])
return;
str += board[x][y];
if (!trie.startsWith(str))
return;
if (trie.search(str))
list.add(str);
visited[x][y] = true;
dfs(list,board, visited, str, x - 1, y, trie);
dfs(list,board, visited, str, x + 1, y, trie);
dfs(list,board, visited, str, x, y - 1, trie);
dfs(list,board, visited, str, x, y + 1, trie);
visited[x][y] = false;
}
}
class TrieNode {
// Initialize your data structure here.
int num;// 有多少单词通过这个节点,即节点字符出现的次数
TrieNode[] son;// 所有的儿子节点
boolean isEnd;// 是不是最后一个节点
char val;// 节点的值
TrieNode() {
this.num = 1;
this.son = new TrieNode[26];
this.isEnd = false;
}
}
class Trie {
protected TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
if (word == null || word.length() == 0)
return;
TrieNode node = this.root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] == null) {
node.son[pos] = new TrieNode();
node.son[pos].val = letters[i];
} else {
node.son[pos].num++;
}
node = node.son[pos];
}
node.isEnd = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
if (word == null || word.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return node.isEnd;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if (prefix == null || prefix.length() == 0) {
return false;
}
TrieNode node = root;
char[] letters = prefix.toCharArray();
for (int i = 0; i < prefix.length(); i++) {
int pos = letters[i] - 'a';
if (node.son[pos] != null) {
node = node.son[pos];
} else {
return false;
}
}
return true;
}
}
Java for LeetCode 212 Word Search II的更多相关文章
- [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] 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 ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- Java实现 LeetCode 212 单词搜索 II(二)
212. 单词搜索 II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中&quo ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
随机推荐
- Win8/Win10无法打开这个应用 内置管理员账户
现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...
- hibernate4连接mysql自动创建表之错误
我在学习Hibernate的过程中,遇到了这样一个错误:JUnit测试通过,但是数据库中却没有创建一个表,控制台的错误信息如下: HHH000388: Unsuccessful: create tab ...
- 2015年12月03日 GitHub入门学习(五)Markdown语法简介
Markdown一种标记语言,语法简洁,不像Word或Pages有大量排版.字体设置.常用的标记符号不超过十个.被大量写作爱好者.撰稿人.作家所青睐. 一.Markdown的优点 专注你的文字内容而不 ...
- Jquery 操作IFrame
使用jquery操作iframe 1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id ...
- AD域服务器|两台DC无法进行复制同步
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 说明:前段时间公司两台域控出现了一些问题导致数据无法相互进行同步,DC之间也无法进行共享访问,网络用户无法通过计算机名映射的共享访问 ...
- myeclipse的项目导入到eclipse下,com.sun.org.apache.commons.beanutils.BeanUtils不能导入
com.sun.org.apache.commons.beanutils.BeanUtils这个包不能引入了怎么办自己下了个org.apache.commons的jar包了之后,改成import or ...
- mysql-mysql优化
mysql数据库优化1.查询优化 (1)避免where 子句中对字段进行 null 值判断 (2)避免在 where 子句中使用 or 来连接条件 (3)少使用like,如果要用可以考虑全文检索 (4 ...
- leetcode 解题报告 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [hihoCoder#1381]Little Y's Tree
[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J ...
- Sqli-LABS通关笔录-3
/*此时心情xxxx*/ 通过这一关卡我学习到了 1.大概的能够mysql回显错误注入的面目,可以根据报错,写出闭合语句. 加一个单引号.报错如下所示. 加了一个单引号就说 1'') LIMIT 0, ...