208. Implement Trie (Prefix Tree)

class TrieNode{
private char val;
public boolean isWord;
public TrieNode[] children = new TrieNode[26];
public TrieNode(){};
public TrieNode(char c){
this.val = c;
}
}
class Trie {
private TrieNode root; /** Initialize your data structure here. */
public Trie() {
root = new TrieNode(' ');
} /** Inserts a word into the trie. */
public void insert(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char c = word.charAt(i);
if(cur.children[c - 'a'] == null){
cur.children[c - 'a'] = new TrieNode(c);
}
cur = cur.children[c - 'a'];
}
cur.isWord = true;
} /** Returns if the word is in the trie. */
public boolean search(String word) {
TrieNode cur = root;
for(int i = 0; i < word.length(); i++){
char c = word.charAt(i);
if(cur.children[c - 'a'] == null) return false;
cur = cur.children[c - 'a'];
}
return cur.isWord;
} /** Returns if there is any word in the trie that starts with the given prefix. */
public boolean startsWith(String prefix) {
TrieNode cur = root;
for(int i = 0; i < prefix.length(); i++){
char c =prefix.charAt(i);
if(cur.children[c - 'a'] == null) return false;
cur = cur.children[c - 'a'];
}
return true;
}
}

211. Add and Search Word - Data structure design

class WordDictionary {

    public class TrieNode{
public TrieNode[] children = new TrieNode[26];
public boolean isWord;
} private TrieNode root = new TrieNode();
/** Initialize your data structure here. */
public WordDictionary() { } /** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode node = root;
for(char c : word.toCharArray()){
if(node.children[c - 'a'] == null){
node.children[c - 'a'] = new TrieNode();
}
node = node.children[c - 'a'];
}
node.isWord = true;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return match(word.toCharArray(), 0, root);
} private boolean match(char[] chs, int start, TrieNode node){
if(start == chs.length) return node.isWord; if(chs[start] == '.'){
for(int i = 0; i < node.children.length; i++){
if(node.children[i] != null && match(chs, start + 1, node.children[i]))
return true;
}
}else{
return node.children[chs[start] - 'a'] != null && match(chs, start + 1, node.children[chs[start] - 'a']);
}
return false;
}
}

<Trie> 208 211的更多相关文章

  1. [leetcode trie]208. Implement Trie (Prefix Tree)

    实现一个字典树 class Trie(object): def __init__(self): self.root = TrieNode() def insert(self, word): cur = ...

  2. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  3. Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式

    递归.二维数组顺时针旋转90°.正则表达式 1.   递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...

  4. des (C语言)

    /** * \file des.h * * \brief DES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...

  5. ZAM 3D 制作简单的3D字幕 流程(二)

    原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...

  6. 通过PowerShell获取Windows系统密码Hash

    当你拿到了系统控制权之后如何才能更长的时间内控制已经拿到这台机器呢?作为白帽子,已经在对手防线上撕开一个口子,如果你需要进一步扩大战果,你首先需要做的就是潜伏下来,收集更多的信息便于你判断,便于有更大 ...

  7. ascii码所有字符对照表(包含汉字和外国文字)

    http://www.0xaa55.com/thread-398-1-1.html看到了0xaa55的这个帖子,想起了2年前我在51cto发的一个帖子http://down.51cto.com/dat ...

  8. DES和3DES加密算法C语言实现【转】

    转自:https://blog.csdn.net/leumber/article/details/78043675 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  9. 导出到Excel中NPOI

    源地址:http://www.cnblogs.com/dreamof/archive/2010/06/02/1750151.html\ 1.NPOI官方网站:http://npoi.codeplex. ...

随机推荐

  1. (二十四)golang--错误处理

    在默认情况下,遇到错误时,程序会崩溃: 在发生错误时,我们可以捕获错误,使程序可以继续运行,并抛出错误提示: 错误处理: (1)Go语言追求简洁优雅,所以不支持传统的try catch finally ...

  2. 用 qemu-user 在arm linux机器上运行amd64/x86程序

    1. qemu-user 是什么 本来, 对于 QEmu, 我只知道它是一个模拟器, 可以像 VirtualBox/VMWare 那样跑一个操作系统, 只不过 QEmu 可以在 AMD64 上面跑针对 ...

  3. 进程间通信的信道与控制(io机制)

    进程间通信 = 信道 + 控制(状态) + io 信道: 1.流式信道: 2.队列信道: 3.共享内存信道: 控制机制: 数据就绪状态的通知与数据获取机制. 1.信号: 2.循环: 3.io机制

  4. SQL Server备份时间段内插入的数据依旧进入了备份文件?(转载)

    问 MSSql我在本机测试了下.为了延长备份时间,找个大的数据库.开始完整备份bak然后再此库新建表,并增添数据.备份结束.==================还原备份后,在还原的数据库内发现新增的表 ...

  5. WebGIS之MapBox篇

    前面在Arcgis的基础上玩了玩,这不最近又去摸索了一下Web上开源的GIS;这次选择了基于MapBox来实现一些效果: 1.加载自己发布的本地瓦片效果 2.加载热力图.Echarts.三位建筑.路况 ...

  6. mysql报错com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

    一.问题 运行java代码时报如下的错误: You have an error in your SQL syntax;Cause:com.mysql.jdbc.exceptions.jdbc4.MyS ...

  7. 如何将LNMP拆分为LNP+MySQL

    1.备份172.16.1.7上的数据库信息 [root@web01 ~]# mysqldump -uroot -p'oldxu.com' --all-databases > mysql-all. ...

  8. MES系统实施4大关键点,您都知道吗?

    MES是制造企业生产管理信息化的核心,能否成功实施和应用MES是企业实现提高生产效率,降低成本等信息化建设目标的关键所在. 但是,对于信息化基础相对薄弱的中国制造企业来说,MES的复杂性使得企业在进行 ...

  9. ANDROID培训准备资料之Service

    在讨论Service 之前,我们需要了解两点,非常重要的两点 (1)     Service 不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中 (2)     Service也不 ...

  10. linux 修改文件打开数量限制

    1.查看打开文件数量限制 ulimit -a ulimit -n 2.临时修改 ulimit -n 2048 3.永久修改 vi /etc/security/limits.conf 追加 * soft ...