题目链接

一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)的更多相关文章

  1. 【leetcode刷题笔记】Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  2. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

  3. 【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 ...

  4. 【leetcode刷题笔记】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. 【刷题-LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Example: ...

  6. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  7. [LeetCode] 208. Implement Trie (Prefix Tree) ☆☆☆

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  8. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  9. 【LeetCode】208. Implement Trie (Prefix Tree)

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

随机推荐

  1. android APP上线前,应该准备的东西

    这里给出一些主流的应用市场名单,有些可能已经不行了,自己找一找,很容易的: 应用市场图-1

  2. SSH电力项目一 搭建Hibernate框架

    Hibernate所需要的基本文件: ElectText.java ElecText.hbm.xml hibernate.cfg.xml 第一步:创建测试表Elec_Text: create tabl ...

  3. 启动原生Hadoop集群或伪分布环境

    一:启动Hadoop 集群或伪分布安装成功之后,通过执行./sbin/start-all.sh启动Hadoop环境 通过jps命令查看当前启动进程是否正确~ [root@neusoft-master ...

  4. order meeting room - 离散度30min

    w <meta charset="UTF-8"> <?php include('conn.php'); include('w_fun.php'); include ...

  5. python 时间与时间戳之间的转换

    https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...

  6. MapReduce分区和排序

    一.排序 排序: 需求:根据用户每月使用的流量按照使用的流量多少排序 接口-->WritableCompareable 排序操作在hadoop中属于默认的行为.默认按照字典殊勋排序. 排序的分类 ...

  7. var 声明变量的变量提升问题(let/const)

    在ES6之前,JavaScript没有块级作用域(一对花括号{}即为一个块级作用域),只有全局作用域和函数作用域.变量提升即将变量声明提升到它所在作用域的最开始的部分.既全局变量. <scrip ...

  8. Install Haskell on Ubuntu and CentOS

    For Ubuntu: Step one: Install GHC If you don't want to install curl you can skip step 1 and just dir ...

  9. 安卓android的联系人的contacts, raw contacts, and data的区别

    https://stackoverflow.com/questions/5151885/android-new-data-record-is-added-to-the-wrong-contact/51 ...

  10. mysql 下的命令

    1.查看mysql日志vim /var/log/mysqld.log