442. Implement Trie (Prefix Tree)

 class TrieNode {
public boolean isWord;
public TrieNode[] children; public TrieNode() {
isWord = false;
children = new TrieNode[26];
} } public class Trie {
private TrieNode root; public Trie() {
// do intialization if necessary
root = new TrieNode();
} /*
* @param word: a word
* @return: nothing
*/
public void insert(String word) {
// write your code here
if (word == null || word.length() == 0) {
return;
}
TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (p.children[index] == null) {
p.children[index] = new TrieNode();
}
p = p.children[index];
}
p.isWord = true;
} public TrieNode find(String prefix) {
if (prefix == null || prefix.length() == 0) {
return null;
}
TrieNode p = root;
for (int i = 0; i < prefix.length(); i++) {
int index = prefix.charAt(i) - 'a';
if (p.children[index] == null) {
return null;
}
p = p.children[index];
}
return p;
} /*
* @param word: A string
* @return: if the word is in the trie.
*/
public boolean search(String word) {
// write your code here
TrieNode p = find(word);
return p != null && p.isWord;
} /*
* @param prefix: A string
* @return: if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String prefix) {
// write your code here
return find(prefix) != null;
}
}

473. Add and Search Word - Data structure design

 class TrieNode {
public boolean isWord;
public char c;
public Map<Character, TrieNode> children; public TrieNode() {
children = new HashMap<>();
} public TrieNode(char c) {
this.c = c;
children = new HashMap<>();
}
} public class WordDictionary {
/*
* @param word: Adds a word into the data structure.
* @return: nothing
*/
TrieNode root = new TrieNode(); public void addWord(String word) {
// write your code here
if (word == null || word.length() == 0) {
return;
} TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (p.children.get(c) == null) {
TrieNode child = new TrieNode();
p.children.put(c, child);
} p = p.children.get(c);
}
p.isWord = true;
} /*
* @param word: A word could contain the dot character '.' to represent any one letter.
* @return: if the word is in the data structure.
*/
public boolean search(String word) {
// write your code here
if (word == null || word.length() == 0) {
return false;
}
TrieNode p = root; for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c != '.') {
if (p.children.get(c) == null) {
return false;
}
p = p.children.get(c);
} else {
for (Map.Entry<Character, TrieNode> entry : p.children.entrySet()) {
if (search(word.substring(0, i) + entry.getKey() + word.substring(i + 1, word.length()))) {
return true;
}
}
return false;
} } return p.isWord;
}
}

132. Word Search II

 class TrieNode {
String word;
Map<Character, TrieNode> children; public TrieNode() {
children = new HashMap<>();
}
} class TrieTree {
TrieNode root; public TrieTree(TrieNode node) {
this.root = node;
} public void insert(String word) {
if (word == null || word.length() == 0) {
return;
}
TrieNode p = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!p.children.containsKey(ch)) {
p.children.put(ch, new TrieNode());
}
p = p.children.get(ch);
}
p.word = word;
}
} public class Solution {
private int[] dx = {0, 1, 0, -1};
private int[] dy = {1, 0, -1, 0}; /**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
public List<String> wordSearchII(char[][] board, List<String> words) {
// write your code here
if (words == null || words.size() == 0) {
return new ArrayList<>();
}
Set<String> res = new HashSet<>();
TrieTree tree = new TrieTree(new TrieNode());
for (String word : words) {
tree.insert(word);
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
dfs(board, i, j, res, tree.root);
}
}
return new ArrayList<>(res);
} public void dfs(char[][] board, int x, int y, Set<String> res, TrieNode node) {
TrieNode child = node.children.get(board[x][y]);
if (child == null) {
return;
}
if (child.word != null) {
if (!res.contains(child.word)) {
res.add(child.word);
}
} char tmp = board[x][y];
board[x][y] = 0;
for (int i = 0; i < dx.length; i++) {
int nxtDx = x + dx[i];
int nxtDy = y + dy[i];
if (!isValid(board, nxtDx, nxtDy)) {
continue;
}
dfs(board, nxtDx, nxtDy, res, child);
}
board[x][y] = tmp;
} public boolean isValid(char[][] board, int x, int y) {
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) {
return false;
}
return board[x][y] != 0;
}
}

最新文章

  1. 编写一个基本的连接池来实现连接的复用&amp;一些工程细节上的优化
  2. 调用webapi 错误:使用 HTTP 谓词 POST 向虚拟目录发送了一个请求,而默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。的解决方案
  3. DAY5 php + mysql 写一个简单的sql注入平台
  4. C#学习笔记-----基于AppDomain的&quot;插件式&quot;开发
  5. 51nod1122 机器人走方格 V4
  6. bzoj1015:[JSOI2008]星球大战starwar
  7. C# 控制台程序 托盘图标 事件响应
  8. mysql中使用concat例子
  9. copy-on-write(写时拷贝技术)
  10. PHP Markdown 解析器Parsedown
  11. Linux 开(关) ICMP 回应(防止被ping)
  12. netty 3.x 实现http server和遇到的坑
  13. Mybatis动态sql及性能优化-3
  14. android 权限
  15. InnoSQL/MySQL并行复制的实现与配置
  16. bzoj3262: 陌上花开 三维偏序cdq分治
  17. maven下载源代码,解决中文注释为乱码的问题
  18. vs2010静态链接Qt
  19. 深入浅出SharePoint——常用的url命令
  20. [HNOI2012]集合选数 --- 状压DP

热门文章

  1. 关于pycharm字体大小的调整
  2. 2.8.3 并发下诡异的HashMap
  3. Java IO输入输出流File 字节流
  4. APUE(3)---文件I/O (2)
  5. HackSix 为ViewGroup的子视图添加悦目的动画效果
  6. 入门训练 Fibonacci数列 (水题)
  7. javascript 数组排序
  8. postgreSQL PL/SQL编程学习笔记(一)
  9. 你必须知道的----C语言笔试面试中经典易错的一些知识点(持续更新)
  10. SDUT OJ 数据结构上机测试1:顺序表的应用