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. Installing R under Unix-alikes

    Linux上R的安装 可参考https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_ ...

  2. 杀毒软件 avg

    http://filehippo.com/download_avg_antivirus_64 R studio: https://www.rstudio.com/products/rstudio/do ...

  3. Robot Framework - 常用断言讲解

    RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便:不能停留在知道或者是会得程度,只有熟练使用各关键字,才能提升自动化用例的写作效率. 下面将逐个 ...

  4. javaweb dom4j解析xml文档

    1.什么是dom4j dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,它 ...

  5. Redis主从服务部署

    Redis__WindowsServer主从服务部署及调用实例       一.先谈谈单个Redis服务的安装         使用的redis是2.8.17版本,从官网下载解压缩后文件内容为:   ...

  6. Thumbnail 图片帮助

    public class Thumbnail { private Image srcImage; private string srcFileName; /// <summary> /// ...

  7. android studio中使用recyclerview小白篇(三)

    继续接着昨天的来,昨天终于弄好了一个例子,但是那个没有点击事件, 需要自己添加,参照别人的例子,弄了个比较简单的,主要是改动myRecycleradatper.java中的部分. 增加如下的接口: / ...

  8. bitest(位集合)------c++程序设计原理与实践(进阶篇)

    标准库模板类bitset是在<bitset>中定义的,它用于描述和处理二进制位集合.每个bitset的大小是固定的,在创建时指定: bitset<4> flags; bitse ...

  9. session相关

    判断session是否已失效: HttpSession session=request.getSession(false); getSession(boolean)相比于getSession()更安全 ...

  10. 【BZOJ2159】Crash的文明世界 斯特林数+树形dp

    Description Crash 小朋友最近迷上了一款游戏--文明5(Civilization V).在这个游戏中,玩家可以建立和发展自己的国家,通过外交和别的国家交流,或是通过战争征服别的国家.现 ...