【LeetCode】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design
Design a data structure that supports the following two operations:
- void addWord(word)
- bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z
or .
. A .
means it can represent any one letter.
For example:
- addWord("bad")
- addWord("dad")
- addWord("mad")
- search("pad") -> false
- search("bad") -> true
- search(".ad") -> true
- search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z
.
- class TrieNode
- {
- public:
- TrieNode* children[];
- bool end;
- TrieNode()
- {
- for(int i = ; i < ; i ++)
- children[i] = NULL;
- end = false;
- }
- };
- class WordDictionary {
- public:
- WordDictionary()
- {
- root = new TrieNode();
- }
- // Adds a word into the data structure.
- void addWord(string word) {
- TrieNode* cur = root;
- int i = ;
- while(i < word.size() && cur->children[word[i]-'a'] != NULL)
- {
- cur = cur->children[word[i]-'a'];
- i ++;
- }
- if(i == word.size())
- cur->end = true;
- else
- {
- while(i < word.size())
- {
- cur->children[word[i]-'a'] = new TrieNode();
- cur = cur->children[word[i]-'a'];
- i ++;
- }
- cur->end = true;
- }
- }
- // Returns if the word is in the data structure. A word could
- // contain the dot character '.' to represent any one letter.
- bool search(string word) {
- return search(word, root);
- }
- bool search(string word, TrieNode* cur)
- {
- if(cur == NULL)
- return false;
- else if(word == "")
- return (cur->end == true);
- else
- {
- if(word[] != '.')
- {
- if(cur->children[word[]-'a'] == NULL)
- return false;
- else
- return search(word.substr(), cur->children[word[]-'a']);
- }
- else
- {
- for(int i = ; i < ; i ++)
- {
- if(search(word.substr(), cur->children[i]))
- return true;
- }
- return false;
- }
- }
- }
- TrieNode* root;
- };
- // Your WordDictionary object will be instantiated and called as such:
- // WordDictionary wordDictionary;
- // wordDictionary.addWord("word");
- // wordDictionary.search("pattern");
【LeetCode】211. Add and Search Word - Data structure design的更多相关文章
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 【刷题-LeetCode】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- [leetcode trie]211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- SAP ABAP编程 取得用户中文名称
有时候我们知道SAP当前用户登录的ID,也就是SY-UNAME.能够取得用户中文名称.例如以下: ***取得用户中文名称 DATA: g_sheet_jsr TYPE string. "用 ...
- Centos curl ssl 替换 NSS 为 OpenSSL
参考:https://www.latoooo.com/xia_zhe_teng/368.htm 我的系统版本是 Centos 7 64位.为了方便,先安装常用的开发环境. yum groupinsta ...
- 求一个正实数X的开方
问题:求一个正实数X的平方根,不能使用sqrt等库函数. 解析:本题要求求一个正实数的平方根,不能使用sqrt等已有的库函数,我们可以做一下考虑: 利用二分法,mid=X/2.0,若mid*mid&g ...
- 成为Linux内核高手的四个方法
首页 最新文章 资讯 程序员 设计 IT技术 创业 在国外 营销 趣文 特别分享 更多 > - Navigation -首页最新文章资讯程序员设计IT技术- Java & Android ...
- Eclipse中GitLab的配置和使用入门
一.Eclipse中配置GitLab的前提条件 1.1:安装Git客户端 去官网https://git-scm.com/downloads下载合适的版本即可,一般开发环境是windows的就下载win ...
- Struts2之server端验证
声明:在我的教程中有些东西,没有提及到.不是我不知道,而是在我个人来看对你们不是太重要的知识点.你们在看课本时有了解到即可.我不会面面俱到的都给你们提及.我写博文的目的是把我这一年的开发经验通过学习s ...
- 未能加载文件或程序集“Microsoft.SqlServer.Sqm, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91”或它的某一个依赖项。系统找不到指定的文件。 (SqlMgmt)
解决方法: Copy this file "Microsoft.SqlServer.Sqm.dll" in the forder "C:/Program Files/Mi ...
- [Algorithm] Check for balanced parentheses using stack
Algorithm or program to check for balanced parentheses in an expression using stack data structure. ...
- 使用Nexus管理maven仓库,setting文件理解
来到新公司对很多陌生的技术一头雾水,以前在工作中没有真正使用过maven,于是强迫自己蛋定下来一个一个的突破,下面是我对maven的setting配置文件的理解,由于是现学的,难免可能会理解偏差,还请 ...
- WIN10系统如何取消右下角的通知菜单,通知图标
鼠标左键单击通知按钮,然后点击所有设置 在通知和操作页面,取消勾选所有的通知 建议选择在任务栏显示哪些图标,然后勾选显示所有图标