[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 ...
随机推荐
- 【BZOJ3289】Mato的文件管理 莫队算法+树状数组
[BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...
- kafka基础概念
kafka介绍 kafka is a distributed, partitiononed,replicated commited logservice. kafka是一个分布式的.易扩展的.安全性高 ...
- window 发布已编译好的ASP文件到IIS
1.进入window 7的控制面板,点击程序,选择程序和功能中的 打开或关闭Windows功能.安装IIS
- Golang学习-第一篇 Golang的简单介绍及Windows环境下安装、部署
序言 这是本人博客园第一篇文章,写的不到位之处,希望各位看客们谅解. 本人一直从事.NET的开发工作,最近在学习Golang,所以想着之前学习的过程中都没怎么好好的将学习过程记录下来.深感惋惜! 现在 ...
- SignalR循序渐进(一)简单的聊天程序
前阵子把玩了一下SignalR,起初以为只是个real-time的web通讯组件.研究了几天后发现,这玩意简直屌炸天,它完全就是个.net的双向异步通讯框架,用它能做很多不可思议的东西.它基于Owin ...
- Bootstrap CSS组组件架构的设计思想
w AO模式 Append Overwrite 附加重写
- python 中几个层次的中文编码.md
转自:[http://swj.me/] 介绍 一直不太喜欢使用命令行,所以去年年底的技术创新中,使用TkInter来开发小工具.结果花费了大量的时间来学习TkInter ui的使用. 最近想整理该工具 ...
- sublime text 3 配置方法
一.安装sublime text 3 1>.执行sublime text 3的安装包(.exe)文件安装成功后,进入sublime的安装目录(例如:D:\Program Files\Sublim ...
- Flask之flask-session
简介 flask-session是flask框架的session组件,由于原来flask内置session使用签名cookie保存,该组件则将支持session保存到多个地方,如: redis:保存数 ...
- SpringMVC读取配置文件
源文件 pay.properties: inputCharset=1 receiveUrl=www.baidu.com version=v1.0 language=1 signType=1 merch ...