[leetcode刷题笔记]Implement Trie (Prefix Tree)
一A,开森~
ac代码:
class TrieNode {
// Initialize your data structure here.
char content;
boolean isWord;
int count;
LinkedList<TrieNode> childList; public TrieNode(char c) {
content = c;
isWord = false;
count = 0;
childList = new LinkedList<TrieNode>();
}
public TrieNode() {
content = ' ';
isWord = false;
count = 0;
childList = new LinkedList<TrieNode>();
} public TrieNode findChildC(char c){
if(childList != null){
for(TrieNode eachChild:childList){
if(eachChild.content == c)
return eachChild;
}
}
return null;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode(); } // Inserts a word into the trie.
public void insert(String word) {
//if already has this word
if(search(word) == true)
return; TrieNode current = root;
for(int i = 0;i < word.length();i++){
TrieNode child = current.findChildC(word.charAt(i));
if(child == null){
TrieNode temp = new TrieNode(word.charAt(i));
current.childList.add(temp);
current = temp;
}else{
current = child;
}
current.count++;
}
current.isWord = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
TrieNode current = root;
for(int i = 0;i < word.length();i++){
TrieNode child = current.findChildC(word.charAt(i));
if(child == null)
return false;
else{
current = child;
continue;
}
}
if(current.isWord)
return true;
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode current = root;
for(int i = 0;i < prefix.length();i++){
TrieNode child = current.findChildC(prefix.charAt(i));
if(child == null)
return false;
else{
current = child;
continue;
}
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
[leetcode刷题笔记]Implement Trie (Prefix Tree)的更多相关文章
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- 【leetcode刷题笔记】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...
- 【leetcode刷题笔记】Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode刷题笔记】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【刷题-LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】208. Implement Trie (Prefix Tree)
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
随机推荐
- @Override错误
导入一个项目,项目所有类报 @Override 有错误,去掉就不报错了,原因?在 Java Compiler 将 Enable project specific setting 选中 然后再选择1 ...
- 让asp.net程序在修改web.config后不重启
默认情况下web.config修改后,asp.net程序会被重启.为了防止程序重启,要修改machine.config 文件. machine.config默认在C:\WINDOWS\Microsof ...
- maven web框架搭建
前面我们描述了如何使用maven搭建一个最基本的Java Project 框架.在实际生产应用中,一般不会仅仅建立一个最基本的Java Project项目,目前 Java Web 开发已经成为Java ...
- Spring + MyBatis中常用的数据库连接池配置总结
Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...
- LeetCode-Lowest Common Ancestor of a Binary Tre
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- jdk1.7访问https报javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure问题解决
本地jdk版本java version "1.8.0_31",代码中已对https做了相应处理:信任所有来源证书,运行正常:上包到服务器(服务器jdk版本java version ...
- Centos7.0安装mysql5.6
环境 CentOS 7.0(64-bit system) mysql-5.6.37 一.mysql依赖libaio,所以要先安装libaio # yum search libaio ...
- sersync+rsync原理及部署
标签:sersync+rsync部署文档 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liubao0312.blog.51ct ...
- Quartz.net 基于配置的调度程序实践
1.Nuget 搜索并安装Quartz.net 2.3.3 2.添加配置到App.config <?xml version="1.0" encoding="utf ...
- Buffer Data
waylau/netty-4-user-guide: Chinese translation of Netty 4.x User Guide. 中文翻译<Netty 4.x 用户指南> h ...