<Trie> 208 211
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的更多相关文章
- [leetcode trie]208. Implement Trie (Prefix Tree)
实现一个字典树 class Trie(object): def __init__(self): self.root = TrieNode() def insert(self, word): cur = ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式
递归.二维数组顺时针旋转90°.正则表达式 1. 递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...
- des (C语言)
/** * \file des.h * * \brief DES block cipher * * Copyright (C) 2006-2010, Brainspark B.V. * * This ...
- ZAM 3D 制作简单的3D字幕 流程(二)
原地址:http://www.cnblogs.com/yk250/p/5663907.html 文中表述仅为本人理解,若有偏差和错误请指正! 接着 ZAM 3D 制作简单的3D字幕 流程(一) .本篇 ...
- 通过PowerShell获取Windows系统密码Hash
当你拿到了系统控制权之后如何才能更长的时间内控制已经拿到这台机器呢?作为白帽子,已经在对手防线上撕开一个口子,如果你需要进一步扩大战果,你首先需要做的就是潜伏下来,收集更多的信息便于你判断,便于有更大 ...
- ascii码所有字符对照表(包含汉字和外国文字)
http://www.0xaa55.com/thread-398-1-1.html看到了0xaa55的这个帖子,想起了2年前我在51cto发的一个帖子http://down.51cto.com/dat ...
- DES和3DES加密算法C语言实现【转】
转自:https://blog.csdn.net/leumber/article/details/78043675 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- 导出到Excel中NPOI
源地址:http://www.cnblogs.com/dreamof/archive/2010/06/02/1750151.html\ 1.NPOI官方网站:http://npoi.codeplex. ...
随机推荐
- centos 启动一个tcp服务程序
需要先yum安装: yum install nc 启动服务: nc -l 80
- Mac终端常用快捷键
Ctrl + a 跳到行首Ctrl + e 跳到行尾Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof)Ctrl + h ...
- Anaconda--在虚拟环境中安装CUDA and cudnn
在conda虚拟环境中安装CUDAconda install cudatoolkit=8.0 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs ...
- Mysql 常用数据类型 占用字节数 [转]
数据类型是定义列中可以存储什么数据以及该数据实际怎么存储的基本规则.Mysql的常用数据类型主要有: 串数据类型:最常用的数据类型,有两种基本的串类型:分别为定长串和不定长串.定长串结束长度固定的字符 ...
- python Lock、RLock
Lock: 只能acquire一次,下一次acquire必须release后才能,不然会造成死锁 from threading import Lock total = 0 lock = Lock() ...
- Java JDK和IntelliJ IDEA 配置及安装
序言 初学java,idea走一波先.安装完成,配置配置项. idea 软件 官方下载地址:https://www.jetbrains.com/idea/download/#section=windo ...
- 个性化你的 Git Log 的输出格式
个性化你的 Git Log 的输出格式
- 永久清理git中的历史大文件
原文发布于:https://www.chenxublog.com/2019/05/26/remove-git-big-files.html 有写老的git仓库,因为当年的无知,不会用.gitignor ...
- 面试官都叫好的Synchronized底层实现,这工资开多少一个月?
本文为死磕Synchronized底层实现第三篇文章,内容为重量级锁实现. 本系列文章将对HotSpot的synchronized锁实现进行全面分析,内容包括偏向锁.轻量级锁.重量级锁的加锁.解锁.锁 ...
- vue实现word,pdf文件的导出功能
vue实现word或pdf文档导出的功能,我的项目是:后端返回一个文档流(下图),然后前端对文档流做处理进行下载,代码如下: import axios from 'axios'; axios.get( ...