Implement Trie(LintCode)
Implement Trie
Implement a trie with insert, search, and startsWith methods.
You may assume that all inputs are consist of lowercase letters a-z.
百度了一下,了解了字典树是啥,然后看了下实现的代码,然后自己写还是不难的(是啊,知道答案当然不会觉得难)。
-
字典树
/**
* Your Trie object will be instantiated and called as such:
* Trie trie = new Trie();
* trie.insert("lintcode");
* trie.search("lint"); will return false
* trie.startsWith("lint"); will return true
*/
class TrieNode {
// Initialize your data structure here.
char val;
boolean isEnd;
int SIZE = 26;
TrieNode[] children; public TrieNode() {
children = new TrieNode[SIZE];
isEnd = false;
}
public TrieNode(char val) {
children = new TrieNode[SIZE];
this.val = val;
isEnd = false;
}
} public class Solution {
private TrieNode root; public Solution() {
root = new TrieNode();
} // Inserts a word into the trie.
public void insert(String word) {
if(word == null || word.length() == 0) return;
char[] cs = word.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++) {
int pos = cs[i] - 'a';
if(p.children[pos] == null) {
p.children[pos] = new TrieNode(cs[i]);
}
p = p.children[pos];
}
if(!p.isEnd) p.isEnd = true;
} // Returns if the word is in the trie.
public boolean search(String word) {
if(word == null || word.length() == 0) return false;
char[] cs = word.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++){
int pos = cs[i] - 'a';
if(p.children[pos] != null) {
p = p.children[pos];
}else return false;
}
return p.isEnd;
} // Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
if(prefix == null || prefix.length() == 0) return false;
char[] cs = prefix.toCharArray();
TrieNode p = root;
for(int i=0;i<cs.length;i++){
int pos = cs[i] - 'a';
if( p.children[pos] != null) {
p = p.children[pos];
}else return false;
}
return true;
}
}
Implement Trie(LintCode)的更多相关文章
- 木材加工(LintCode)
木材加工 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 样例 有3根木头[232 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- 判断数独是否合法(LintCode)
判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填 ...
- 二进制求和(LintCode)
二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 细节出了好多问题,提交了好多次... public class Solution { / ...
- leetcode 之Implement strStr()(27)
字符串的匹配,返回匹配开始的位置,直接用暴力方式求解.为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可 直接停止匹配. char *strStr(char *haystack, ...
- 丑数(LintCode)
丑数 设计一个算法,找出只含素因子3,5,7 的第 k大的数. 符合条件的数如:3,5,7,9,15...... 您在真实的面试中是否遇到过这个题? Yes 样例 如果k=4, 返回 9 挑战 要求时 ...
- Word Ladder(LintCode)
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- Container With Most Water(LintCode)
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- Single Number II(LintCode)
Single Number II Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Examp ...
随机推荐
- Elasticsearch 5.6.5 安装教程
下载地址 https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-5 安装环境 centos6.5 , jdk1.8 ...
- WebDriver中如何处理Iframe 及 嵌套Iframe
最近在用webdriver进行爬虫的时候,遇到了网站存在iframe的情况,处理了好久没有解决,后来发现原来webdriver自带处理方法,汗颜.. 1.iFrame有ID 或者 name的情况 // ...
- Markdown 代码块中再内嵌一个行内代码
在 jQuery 1.9 之前(不含1.9):如果传入一个空字符串. null 或 jQuery.parseJSON( jsonString ) ,该函数将返回,而不是抛出一个错误,即使它不是有效的 ...
- 验证组件——FluentValidation
FluentValidation FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开 ...
- log4net 性能测试
1.执行事务:20260 次 写日志: 耗时11.59分 不写日志: 耗时11.55分 异步日志: 耗时12.49分 (个人电脑,.net 线程池调用线程写日志可能比主线程直 ...
- javascript拖拽原理与简单实现方法[demo]
美国人有一句常用的俗语—“Re-inventing the Wheel”,从字面上来解释就是“重新发明轮子”.可是轮子早已问世,再要去发明岂非劳而无功? 产品经理发下需求,实施者再到网上搜索代码,也许 ...
- Jmeter-8-FTP测试
1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...
- Spring 框架的设计理念与设计模式分析(山东数漫江湖)
Spring 的骨骼架构 Spring 总共有十几个组件,但是真正核心的组件只有几个,下面是 Spring 框架的总体架构图: 图 1 .Spring 框架的总体架构图 从上图中可以看出 Spring ...
- hdu 1281 棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) M ...
- 【转】gif文件格式详解
1.概述 ~~~~~~~~ GIF(Graphics Interchange Format,图形交换格式)文件是由 CompuServe公司开发的图形文件格式,版权所有,任何商业目的使用均须 Comp ...