<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. ...
随机推荐
- 使用JaCoCo统计单元测试代码覆盖率
1 JaCoCo介绍 JaCoCo是EclEmma团队基于多年覆盖率库使用经验总结而研发的一个开源的Java代码覆盖率库. 2 JaCoCo覆盖率计数器 JaCoCo 包含了多种尺度的覆盖率计数器(C ...
- Jupyter notebook中的.ipynb文件转换成python的.py文件
转自:https://blog.csdn.net/wyr_rise/article/details/82656555 Jupyter notebook中.py与.ipynb文件的import问题 ...
- 错误解决:android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicker.TimePicker
今天在做项目开发时遇到这么一个错误,完整的错误提示信息如下: java.lang.RuntimeException: Unable to start activity ComponentInfo{co ...
- vue使用--Jquery引入
为什么要引入jquery? vue中一般不需要使用jquery,但当我们需要使用的某个插件没有vue的版本且又使用了jquery,那我们就需要引入jquery了 安装.配置与使用 ①insta ...
- 数据仓库002 - 复习Linux shell命令 - echo bash_profile bashrc which命令的理解 alias history
1.echo 打印 . echo 的作用是在屏幕上打印输出内容,与文件和持久化可以理解为没有丝毫关联.如:在屏幕上打印“ echo 的作用是打印文字! ” 实例1:输出系统的环境变量名称 $PATH ...
- fiddler抓包-7-C端弱网测试
前言大家平时也会发现我们有时候在地铁.高铁.电梯等等某个时候网络信号比较差导致网络延迟较大,这时是否有友好提示呢?甚至有可能发生崩溃等等...所以我们是可以通过fiddler来对web.APP.PC客 ...
- vue项目打包之后样式错乱问题,如何处理
最近公司做的这个项目,要大量修改element里面的css样式,所以项目打包之后 会出现样式和本地开发的时候样式有很多不一样,原因可能是css加载顺序有问题,样式被覆改了. 所以在mian.js里面这 ...
- 转:对softmax讲解比较清楚的博客
https://blog.csdn.net/wgj99991111/article/details/83586508
- CentOS下安装FreeTDS
导读 官方网站:http://www.freetds.org 下载地址:http://ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable ...
- net core中引用GDAL
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</Ou ...