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字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
随机推荐
- 使用 Sentry集中处理错误
Sentry的简介 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将程序的所有 exception 自动记录下来,处理 exception 是每个程 ...
- Celery笔记
异步任务神器 Celery 简明笔记 2016/12/19 · 工具与框架 · Celery, 异步 原文出处: FunHacks 在程序的运行过程中,我们经常会碰到一些耗时耗资源的操作,为了避 ...
- 跨域Ajax请求(jQuery JSONP MVC)
通过jQuery的$.ajax方法发送JSONP请求 js代码 <script type="text/javascript"> function jsonptest2( ...
- OM—>AR相关会计科目
业务会计核算 挑库: 借:发出商品 (递延销货成本) 贷:发出商品 (递延销货成本) 发运: 借:发出商品 (递延销货成本) 贷:库存商品/原材料 (库存估 ...
- 将“100px” 转换为100
parseInt("100px") //结果是100
- fabric Clone
记录下: var newObj = fabric.util.object.clone(obj); decDoc.dropCanvas.add(newObj., top: }));
- Log--检查各数据库日志的使用情况
-- Recovery model, log reuse wait description, log file size,-- log usage size and compatibility lev ...
- webform Response的一些成员
1. Response.BufferOutPut,关闭缓冲区. 2. Response.Flush,一次性把缓冲区的内容释放出来. 3. Response.Clear,清空缓冲区. 4. Respon ...
- 基于CentOS6定制自己的ISO安装光盘
警告:转载请注明出处 https://www.cnblogs.com/BoyTNT/p/9322927.html 1.目标 >> 基于CentOS-6.10-x86_64-minimal ...
- Mysql避免重复插入记录方法
一.mysql replace用法 1.replace into replace into table (id,name) values('1','aa'),('2','bb') 此语句的作用是向 ...