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 ...
随机推荐
- C11简洁之道:lambda表达式
1. 定义 lambda表达式是C++11非常重要也是很常用的特性之一,来源于函数式编程的概念,也是现代编程语言的一个特点.它有如下特点: 声明式编程风格:就地匿名定义目标函数或者函数,不需要额外写 ...
- WCF 同一个解决方案中控制台应用添加服务引用报错
错误提示: “Unable to check out the current file. The file may be read-only or locked, or you may need to ...
- VirtualBox4.3.12 安装ubuntu 14.04 分辨率过小(600*480)问题的解决方法
作为.net程序员,一直都跟windows系统打交道,在同事的影响下,今天安装了Ubuntu 14. 安装完系统就遇到了这个麻烦事,找了好久才解决,因此记录下来,或许对和我一样的Ubuntu新手有帮助 ...
- CCD和CMOS的差别
单从感光器电子技术上来说,CCD比CMOS更先进,理论成像上有优势,但是最近几年CMOS却发展更好,使得很多高端数码单反采用CMOS传感器,下面来看看CCD和CMOS的技术知识: CCD和CMOS传感 ...
- 【Atcoder】CODE FESTIVAL 2017 qual A D - Four Coloring
[题意]给定h,w,d,要求构造矩阵h*w满足任意两个曼哈顿距离为d的点都不同色,染四色. [算法]结论+矩阵变换 [题解] 曼哈顿距离是一个立着的正方形,不方便处理.d=|xi-xj|+|yi-yj ...
- 【bzoj】1927 [Sdoi2010]星际竞速
[算法]最小费用最大流 [题解]跟滑雪略有类似,同样因为可以重复所以不是最小路径覆盖. 连向汇的边容量为1足矣,因为一个点只会出去一次(路径结束). bzoj 1927 [Sdoi2010]星际竞速 ...
- cnn 卷积神经网络 人脸识别
卷积网络博大精深,不同的网络模型,跑出来的结果是不一样,在不知道使用什么网络的情况下跑自己的数据集时,我建议最好去参考基于cnn的手写数字识别网络构建,在其基础上进行改进,对于一般测试数据集有很大的帮 ...
- 深入理解 JavaScript(四)
前言 Bob 大叔提出并发扬了 S.O.L.I.D 五大原则,用来更好地进行面向对象编程,五大原则分别是: The Single Responsibility Principle(单一职责 SRP) ...
- python中的ftplib模块
前言 Python中默认安装的ftplib模块定义了FTP类. ftplib模块相关参数: 加载ftp模块:from ftplib import FTP ftp = FTP()#设置变量ftp.set ...
- nginx 伪静态rewrite
location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...