Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

实现一个字典树。

好久不做题,没感觉啊,TreeNode用一个布尔变量表示是否是一个合法单词的结尾即可,一开始还用cnt来计数,search的时候比较麻烦。

class TrieNode {
// Initialize your data structure here.
TrieNode [] next;
boolean valid;
public TrieNode() {
next = new TrieNode[26];
valid=false;
}
} public class Trie {
private TrieNode root; public Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr.next[c-'a']==null){
ptr.next[c-'a'] = new TrieNode();
}
ptr=ptr.next[c-'a'];
}
ptr.valid=true;
} // Returns if the word is in the trie.
public boolean search(String word) {
int last = 0;
TrieNode ptr = root;
for(char c:word.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return ptr.valid;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode ptr = root;
for(char c:prefix.toCharArray()){
if(ptr==null||ptr.next[c-'a']==null){
return false;
}
ptr=ptr.next[c-'a'];
}
return true;
}
} // Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");

Implement Trie (Prefix Tree) ——LeetCode的更多相关文章

  1. Implement Trie (Prefix Tree) - LeetCode

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

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

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

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

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

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

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

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

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

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

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

  7. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  8. 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...

  9. Leetcode: Implement Trie (Prefix Tree) && Summary: Trie

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

随机推荐

  1. web 页面传值方法

    一. 使用QueryString变量    QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数 ...

  2. HTML5 Media事件

    Media 事件 由媒介(比如视频.图像和音频)触发的事件(适用于所有 HTML 元素,但常见于媒介元素中,比如 <audio>.<embed>.<img>.< ...

  3. PHP上传原理及应用

    概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...

  4. CI 笔记4 (easyui 手风琴)

    添加父div标签,和子div标签 <div class="easyui-accordion" data-options="fit:true,border:false ...

  5. 将日期和时间作为 struct tm型的值直接向二进制文件进行读写

    #include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; v ...

  6. xml 个人练习2

    package cn.gdpe.xml; import java.io.File;import java.io.FileInputStream;import java.io.IOException;i ...

  7. 子元素的margin-top影响父元素原因和解决办法

    这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...

  8. 前端html+css之第十四天

    一.HTML 1.HTML是什么? Hypertext Markup Language, 中文也就是超文本链接标示语言. HTML是一套规则,一套浏览器认识的规则. 2.开发者: (1)学习Html规 ...

  9. var a =a || {}

  10. arm-none-eabi-gcc install

    Zephyr除了官方的编译工具,还有第三方工具 arm-none-eabi-gcc . This PPA is an alternative to toolchain released at http ...