Trie - 20181113
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;
}
}
Trie - 20181113的更多相关文章
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- URAL 1356. Something Easier(哥德巴赫猜想)
题目链接 题意 : 给你一个数n,让你找出几个素数,使其相加为n,输出这些素数. 思路 : 哥德巴赫猜想 : 任何一个大于 6的偶数都可以表示成两个素数之和. 任何一个大于9的奇数都可以表示成三个素数 ...
- PostgreSQL 速查、备忘手册 | PostgreSQL Quick Find and Tutorial
PostgreSQL 速查.备忘手册 作者:汪嘉霖 这是一个你可能需要的一个备忘手册,此手册方便你快速查询到你需要的常见功能.有时也有一些曾经被使用过的高级功能.如无特殊说明,此手册仅适用于 Linu ...
- 设计模式11: Flyweight 享元模式(结构型模式)
Flyweight 享元模式(结构型模式) 面向对象的代价 面向对象很好的解决了系统抽象性的问题,同时在大多数情况下也不会损及系统的性能.但是,在某些特殊应用中,由于对象的数量太大,采用面向对象会给系 ...
- 关于IIS配置SimpleHandlerFactory-Integrated在其模块列表中有一个错误模块ManagedPipelineHandler的错误处理
解决方法: 使用管理员运行aspnet_regiis.exe 命令:%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i v ...
- 严选 Android 路由框架优化(下篇)
3 router 框架优化 3.1 apt 生成代码量过大问题优化 思考框架本身,其实可以发现仅有 router 映射表是需要根据注解编译生成的,其他的全部代码都是固定代码,完全可以 sdk 中直接编 ...
- 为openstack服务使能debug模式
Most OpenStack services use the same configuration options to enable the debug logging that is also ...
- 【leetcode 138. 复制带随机指针的链表】解题报告
方法一:递归 unordered_map<Node*,Node*> dict; Node* copyRandomList(Node* head) { if (!head) return h ...
- 【bzoj1009】: [HNOI2008]GT考试 字符串-kmp-矩阵乘法-DP
[bzoj1009]: [HNOI2008]GT考试 先用kmp写个暴力 /* http://www.cnblogs.com/karl07/ */ #include <cstdlib> # ...
- oracle转义用单引号
参考:https://blog.csdn.net/learning_oracle_lh/article/details/46639507
- .NET clickonce修改发布名称等
见图