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. PMP132种工具与技术

    <PMBOK ® 指南>第六版中共包括 132 种工具与技术 <PMBOK ® 指南>使用了以下工具与技术分组:1.数据收集技术.用于从各种渠道收集数据与信息.共有9 种数据收 ...

  2. H5 背景图片自适应屏幕问题解决办法

    今天也是偶然遇到这个问题的,平成很少用到关于css的内容,就在网上找了很多个方法,最后总结以下几种很实用的方法 方法1: <style> body{ background:url(beij ...

  3. POJ 3252 (数位DP)

    ###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...

  4. 如何安装redis

    主要方式有四种:1.使用 Docker 安装.2.通过 Github 源码编译.3.直接安装 apt-get install(Ubuntu).yum install(RedHat) 或者 brew i ...

  5. 基于OceanStor Dorado V3存储之精简高效 Smart 系列特性

    基于OceanStor Dorado V3存储之精简高效 Smart 系列特性 1.1  在线重删 1.2  在线压缩 1.3  智能精简配置 1.4  智能服务质量控制 1.5  异构虚拟化 1.6 ...

  6. linux部署java命令

    启动: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log &q ...

  7. virtualbox的安装与使用、安装镜像创建虚拟机

    1.官网:https://www.virtualbox.org/ 然后呢,点击下载: 开始安装virtualbox: 双击安装.详细安装过程见:https://baijiahao.baidu.com/ ...

  8. Hyper-V + CentOS7 安装教程(视频)

    (双击全屏播放) 一.前言 为什么选择Hyper-V? windowns自带,免费 基础环境 二.虚拟机配置 下载CentOS7镜像 https://www.centos.org/download/ ...

  9. 原生JavaScript HTML DOM Style 对象参考

    Style 对象属性 可以在Style对象上使用以下属性: “CSS”列指示定义属性的CSS版本(CSS1,CSS2或CSS3). 属性 描述 CSS alignContent 当项目不使用所有可用空 ...

  10. React的jsx语法,详细介绍和使用方法!

    jsx语法 一种混合使用html及javascript语法的代码 在js中 遇到<xx>即开始html语法 遇到</xx>则结束html语法 恢复成js语法 例如: let D ...