leetcode208
class TrieNode {
public:
// Initialize your data structure here.
TrieNode() {
words=;
prefixs=;
for(int i=;i<;i++)
edges[i]=NULL;
}
int words;
int prefixs;
TrieNode* edges[];
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
insertHelper(root,word,);
} // Returns if the word is in the trie.
bool search(string word) {
return searchHelper(root,word,)!=;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
return startsWithHelper(root,prefix,)!=;
} void insertHelper(TrieNode * node,string &word,int pos) {
if(pos==word.size())
{
node->words++;
node->prefixs++;
}
else
{
node->prefixs++;
int char_code=word[pos]-'a';
if(node->edges[char_code]==NULL)
node->edges[char_code]=new TrieNode();
insertHelper(node->edges[char_code],word,pos+);
}
} int searchHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->words;
else if(node->edges[char_code]==NULL)
return ;
else
return searchHelper(node->edges[char_code],word,pos+);
} int startsWithHelper(TrieNode * node,string &word,int pos)
{
int char_code=word[pos]-'a';
if(pos==word.size())
return node->prefixs;
else if(node->edges[char_code]==NULL)
return ;
else
return startsWithHelper(node->edges[char_code],word,pos+);
} private:
TrieNode* root;
}; /**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/
补充一个python的实现:
class TrieNode:
def __init__(self):
self.words = 0
self.prefixs = 0
self.edges = [None] * 26 class Trie:
def __init__(self):
self.root = TrieNode() def insert(self,word):
self.insertHelper(self.root,word,0) def search(self,word):
return self.searchHelper(self.root,word,0) != 0 def startsWith(self,prefix):
return self.startsWithHelper(self.root,prefix,0) != 0 def insertHelper(self,node,word,pos):
if pos == len(word):
node.words += 1
node.prefixs += 1
else:
node.prefixs += 1
char_code = ord(word[pos]) - 97
if node.edges[char_code] == None:
node.edges[char_code] = TrieNode()
self.insertHelper(node.edges[char_code],word,pos+1) def searchHelper(self,node,word,pos):
if pos == len(word):
return node.words
else:
char_code = ord(word[pos]) - 97
if node.edges[char_code] == None:
return 0
else:
return self.searchHelper(node.edges[char_code],word,pos+1) def startsWithHelper(self,node,word,pos):
if pos == len(word):
return node.prefixs
else:
char_code = ord(word[pos]) - 97
if node.edges[char_code] == None:
return 0
else:
return self.startsWithHelper(node.edges[char_code],word,pos+1)
leetcode208的更多相关文章
- leetcode208 happynumber
19 is a happy number 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 class Solution {public ...
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- LeetCode208:Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
随机推荐
- Building tools 为什么是主流?
一.building tools 为什么主流? Gradle 是目前比较流行的构建工具之一,Android Studio 中集成的就是 Gradle,并针对 Android 应用开发了插件 Gradl ...
- 使用Java API方式连接HDFS Client测试
IDEA中新建Maven工程,添加POM依赖, 在IDE的提示中, 点击 Import Changes 等待自动下载完成相关的依赖包. <?xml version="1.0" ...
- js带“.”的对象属性名怎么使用
问题:这样的json对象(event) { "title": "title", "alert":"ding", &quo ...
- JavaBasic_12
类 抽象:对象的共性 类:产生对象的模板(对于属性来讲,类规定有没有属性以及属性类型,具体的属性值因对象的不同而不同) 数据类型:数据集及基于这个数据集的操作 类:我们自定义类型(定义成员变量,基于数 ...
- 温度转换-python
#接收一段字符串 TempStr = input("请输入带有符号的温度值:") #如果 TS 最后一位是大写或是小写 f if TempStr[-1] in ['F','f']: ...
- leetcode 421.Maximum XOR of Two Numbers in an Array
题目中给定若干个数,然后任意选定两个数使得其异或值最大. 先利用样例中的: 3 10 5 25 2 8 这些数转换为二进制来看的话那么是先找到最高位的1然后与数组中其他的数相与后的数值保存到set中去 ...
- vue学习笔记——路由
1 路由配置 在vue.config中配置,则在代码中可以使用 @来表示src目录下 import aa from '@/aa/index.js' 2 单页面可以懒加载 3 创建动态路由 路由中定义: ...
- Linux内核分析第六次作业
分析system_call中断处理过程 一.先在实验楼的虚拟机中MenuOs增加utsname和utsname-asm指令. 具体实现如下: 1.克隆最新新版本的menu,之后进入menu 2.进入t ...
- 串口发送端verilog代码分析
串口发送端verilog代码分析 `timescale 1ns / 1ps ////////////////////////////////////////////////////////////// ...
- Delphi XE5 Android 调用 Google ZXing
{ Google ZXing Call demo Delphi Version: Delphi XE5 Version 19.0.13476.4176 By: flcop(zylove619@hotm ...