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的更多相关文章

  1. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  2. 基于trie树的具有联想功能的文本编辑器

    之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...

  3. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  4. hihocoder-1014 Trie树

    hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  7. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  9. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

随机推荐

  1. PyV8在服务端运行自动崩溃问题

    近来想在服务端架设WSGI + PyV8去自动解析JavaScript代码,然后返回解析后的数据给客户端.但是发现,在nginx配置后,客户端一请求,服务端的python脚本自动崩溃. 见代码: de ...

  2. OSG3.2+Qt5.2.1+VS2012+OSGEarth 2.5编译问题记录

    问题1:CMake Error at D:/Qt/Qt5.2.1/5.2.1/msvc2012_64_opengl/lib/cmake/Qt5Gui/Qt5GuiConfigExtras.cmake: ...

  3. python3安装 feedparser

    在看<集体智慧编程>时碰到python3环境下安装feedparser的问题,搜索发现很多人碰到此问题,最终找以下方法解决. how to install feedparser on py ...

  4. 【Android学习】Android工程资源命名禁忌

    在制作一个继续按钮时,将button的id设置为continue,发现报了错误,error: invalid symbol: 'continue' 一开始还以为是编码问题,后来百度之后才知道安卓And ...

  5. Ubuntu设置root账户密码

    创建Ubuntu后是没有root账户的,执行 sudo passwd root 然后系统会提示你输入普通用户的密码.输入后,按回车,然后重复输入两次新的root密码即可激活root用户.

  6. Django不能使用ip方式访问的解决办法

    问题: 启动服务后,使用http://127.0.0.1:8000/showcase/或者http://localhost:8000/showcase/都能访问, 但是使用http://192.168 ...

  7. 20165219第4次实验《Android程序设计》实验报告

    20165219第4次实验<Android程序设计>实验报告 一.实验内容及步骤 (一)Android Stuidio的安装Hello world测试 要求 参考http://www.cn ...

  8. 857. Minimum Cost to Hire K Workers

    There are N workers.  The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...

  9. win10 + Lubuntu 双系统安装

    win10 + Lubuntu 双系统安装 最近重装了系统,索性直接安装win10 + Lubuntu 双系统,便于在物理机下进行 Linux开发. 这里我选择的 Linux 发行版是 Lubuntu ...

  10. ubuntu15.04下安装docker

    ​##获得更多资料欢迎进入我的网站或者 csdn或者博客园 最近听说docker很火,不知道什么东西,只知道是一个容器,可以跨平台.闲来无事,我也来倒弄倒弄.本文主要介绍:ubuntu下的安装,以及基 ...