[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 ...
随机推荐
- 第七篇:使用 CUDA 进行计算优化的两种思路
前言 本文讨论如何使用 CUDA 对代码进行并行优化,并给出不同并行思路对均值滤波的实现. 并行优化的两种思路 思路1: global 函数 在 global 函数中创建出多个块多个线程对矩阵每个元素 ...
- 在静态工具类中需要注入mapper
在xml中 <bean id="messageUtil" class="org.ldd.ssm.hangyu.utils.MessageUtil" ini ...
- 加载CDN加速服务地址
Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影.很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快.之前火端网络的一些网站都是使用Google的jq ...
- 修改DedeCMS图片上传路径命名规则的具体方法步骤
收藏到:0时间:2013-08-23 文章来源:马海祥博客 访问次数:2350 最近在整理网站根目录下文件的时候,发现马海祥博客网站已经有上千个文件夹了,其中光图片文件夹就占了近一半.这个主要 ...
- ionic 移动开发性能调优-去除动画
<ion-refresher></ion-refresher> ion-refresher指令有以下可选的属性: on-refresh - 当用户向下拉动足够的距离并松开时,执 ...
- 反编译apk + eclipse中调试smali
1.对apk使用apktool反编译出可调试的smali代码到out文件夹 apktool -d d 定点加粉丝_com.mingniu.wxddjfs_440.apk -o out 这里必须使用-d ...
- FAT AP 与 FIT AP的特点和区别
Fat AP的主要特点: Fat AP是与Fit AP相对来讲的, Fat AP将WLAN的物理层.用户数据加密.用户认证.QoS.网络管理.漫游技术以及其他应用层的功能集于一身. Fat AP无线网 ...
- 剑指Offer——二叉搜索树的第k个结点
题目描述: 给定一颗二叉搜索树,请找出其中的第k大的结点. 例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4 分析: 二叉搜索树中序遍历就是从小到大.只 ...
- CF #301 A :Combination Lock(简单循环)
A :Combination Lock 题意就是有一个密码箱,密码是n位数,现在有一个当前箱子上显示密码A和正确密码B,求有A到B一共至少需要滚动几次: 简单循环:
- js的class属性获取、增加、移除
2018年4月10日,北京城的第三份工作已经开始,坚信自己在这里能学到很多,加油! 贴代码,昨天回顾了一点js知识: <script> $(function(){ //赋予一个点击事件 $ ...