当前位置: 首页 > news >正文

LeetCode——Implement Trie (Prefix Tree)

Description:

Implement a trie with insertsearch, and startsWith methods.

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

实现一个简单的Trie树,参考我的博客:http://www.cnblogs.com/wxisme/p/4876197.html

 

 1 class TrieNode {
 2     public TrieNode[] son;
 3     public boolean isEnd;
 4     // Initialize your data structure here.
 5     public TrieNode() {
 6         this.son = new TrieNode[26];
 7         isEnd = false;
 8         
 9     }
10 }
11 
12 public class Trie {
13     private TrieNode root;
14 
15     public Trie() {
16         root = new TrieNode();
17     }
18 
19     // Inserts a word into the trie.
20     public void insert(String word) {
21         char[] wordChars = word.toCharArray();
22         TrieNode node = this.root;
23         for(char ch : wordChars) {
24             int pos = ch - 'a';
25             if(node.son[pos] == null) {
26                 node.son[pos] = new TrieNode();
27             }
28             node = node.son[pos];
29         }
30         node.isEnd = true;
31     }
32 
33     // Returns if the word is in the trie.
34     public boolean search(String word) {
35         char[] wordChars = word.toCharArray();
36         TrieNode node = this.root;
37         for(char ch : wordChars) {
38             int pos = ch - 'a';
39             if(node.son[pos] == null) {
40                 return false;
41             }
42             node = node.son[pos];
43         }
44         return node.isEnd;
45     }
46 
47     // Returns if there is any word in the trie
48     // that starts with the given prefix.
49     public boolean startsWith(String prefix) {
50         
51         char[] prefixChars = prefix.toCharArray();
52         TrieNode node = this.root;
53         for(char ch : prefixChars) {
54             int pos = ch - 'a';
55             if(node.son[pos] == null) {
56                 return false;
57             }
58             node = node.son[pos];
59         }
60         return true;
61     }
62 }
63 
64 // Your Trie object will be instantiated and called as such:
65 // Trie trie = new Trie();
66 // trie.insert("somestring");
67 // trie.search("key");

 

相关文章:

  • 从普通程序员到身价过百亿:追求长期价值的耐心,决定了你能走多远
  • Android图形显示系统——概述
  • WPF中查看PDF文件
  • Jenkins——持续集成服务器
  • JVM里面hashtable和hashmap实现原理
  • iOS 10 的推送 User Notifications Framework
  • .NET连接MongoDB数据库实例教程
  • rar自动压缩备份
  • Java_BigDecimal类型比较大小
  • 小程序使用smart模板的方法
  • LoadRunner上传文件脚本
  • Android自定义view双缓存技术
  • Linux命令行下运行java.class文件
  • nmap入门之其他
  • 实现IOC功能的简单Spring框架
  • 【391天】每日项目总结系列128(2018.03.03)
  • 30秒的PHP代码片段(1)数组 - Array
  • Android开源项目规范总结
  • Angularjs之国际化
  • Linux快速复制或删除大量小文件
  • MySQL用户中的%到底包不包括localhost?
  • nfs客户端进程变D,延伸linux的lock
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • SpiderData 2019年2月23日 DApp数据排行榜
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • WinRAR存在严重的安全漏洞影响5亿用户
  • 包装类对象
  • 闭包,sync使用细节
  • 老板让我十分钟上手nx-admin
  • 排序算法学习笔记
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 三栏布局总结
  • 小试R空间处理新库sf
  • 协程
  • 一起来学SpringBoot | 第三篇:SpringBoot日志配置
  • 异常机制详解
  • ​用户画像从0到100的构建思路
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • $refs 、$nextTic、动态组件、name的使用
  • (0)Nginx 功能特性
  • (8)STL算法之替换
  • (9)STL算法之逆转旋转
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (ros//EnvironmentVariables)ros环境变量
  • (二)WCF的Binding模型
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (十八)三元表达式和列表解析
  • (十一)手动添加用户和文件的特殊权限
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (转)JAVA中的堆栈
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET Remoting学习笔记(三)信道