Trie for string LeetCode
Trie build and search
class TrieNode
{
public:
TrieNode * next[];
bool is_word;
TrieNode(bool b = false)
{
memset(next,,sizeof(next));
is_word = b;
}
};
class Trie {
TrieNode* root;
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode* p = root;
for(int i=;i<word.size();i++)
{
if(p->next[word[i]-'a']==NULL)
p->next[word[i]-'a']=new TrieNode();
p = p->next[word[i]-'a'];
}
p->is_word=true;
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* p = find(word);
return p&&p->is_word;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
return find(prefix);
} TrieNode* find(string word)
{
TrieNode* p = root;
for(int i=;i<word.size();i++)
if(p->next[word[i]-'a']==NULL)return NULL;
else p=p->next[word[i]-'a'];
return p;
}
}; /**
* 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);
*/
加 . 正则匹配一个任意字母 ,递归处理,注意p应指向当前遍历字母对应的结点
class TrieNode
{
public:
TrieNode *next[];
bool is_word; TrieNode(bool b = false)
{
memset(next, , sizeof(next));
is_word = b;
}
}; class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
root = new TrieNode();
} /** Adds a word into the data structure. */
void addWord(string word) {
TrieNode *p = root;
for (int i = ; i < word.size(); i++)
{
if (p->next[word[i] - 'a'] == NULL)
p->next[word[i] - 'a'] = new TrieNode();
p = p->next[word[i] - 'a'];
}
p->is_word = 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 query(word.c_str(), root);
} private:
TrieNode* root;
bool query(const char* word, TrieNode* node)
{
TrieNode* p = node;
for (int i = ; word[i]; i++)
{
if (p && word[i] != '.')
p = p ->next[word[i] - 'a'];
else if (p && word[i] == '.')
{
TrieNode* tmp=p;
for (int j = ; j < ; j++)
{
p = tmp -> next[j];
if (query(word + i + , p))
return true;
}
}
else break;
}
return p && p -> is_word;
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/
word search II
1. word list insert to Trie
2. dfs search
class TrieNode
{
public:
TrieNode *next[];
string word; TrieNode()
{
memset(next, , sizeof(next));
word = "";
}
}; class Trie
{ public:
TrieNode* root;
Trie()
{
root = new TrieNode();
} void insert(string s)
{
TrieNode *p = root;
for (int i = ; i < s.size(); i++)
{
if (p->next[s[i] - 'a'] == NULL)
p->next[s[i] - 'a'] = new TrieNode();
p = p->next[s[i] - 'a'];
}
p->word = s;
} }; void dfs(int i, int j, TrieNode* p, vector<vector<char>>& board, vector<string> &res)
{
char c = board[i][j];
if (c == '#' || p->next[c - 'a'] == NULL)return;
p = p->next[c - 'a'];
if (p->word != "")
{
res.push_back(p->word);//找到
p->word = "";//去重
}
board[i][j] = '#';
if (i > )dfs(i - , j, p, board, res);
if (j > )dfs(i, j - , p, board, res);
if (i < board.size() - )dfs(i + , j, p, board, res);
if (j < board[].size() - )dfs(i, j + , p, board, res);
board[i][j] = c;
} class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie t;
vector<string> res;
for (int i = ; i < words.size(); i++)
t.insert(words[i]);
for (int i = ; i < board.size(); i++)
for (int j = ; j < board[].size(); j++)
dfs(i, j, t.root, board, res);
return res;
}
};
421 数组中任意两个数的最大异或值
思路:建树插入所有数,对每个数按位查找异或值
class Trie{
public:
Trie* children[];
Trie(){
children[]=NULL;
children[]=NULL;
}
};
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
if(nums.size()==)return ;
//Init Trie
Trie* root = new Trie();
for(int num:nums)
{
Trie* curNode = root;
for(int i=;i>=;i--)
{
int curBit = (num>>i)&;
if(curNode->children[curBit]==NULL)
{
curNode->children[curBit] = new Trie();
}
curNode = curNode->children[curBit];
}
}
int _max = 0xffffffff;
for(int num:nums)
{
Trie* curNode = root;
int curSum = ;
for(int i=;i>=;i--)
{
int curBit = (num>>i)&;
if(curNode->children[curBit^]!=NULL)
{
curSum += <<i;
curNode = curNode->children[curBit^];
}
else
{
curNode = curNode->children[curBit];
}
}
_max = max(curSum,_max);
}
return _max;
}
};
Trie for string LeetCode的更多相关文章
- Implement Trie (Prefix Tree) ——LeetCode
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- Scramble String -- LeetCode
原题链接: http://oj.leetcode.com/problems/scramble-string/ 这道题看起来是比較复杂的,假设用brute force,每次做分割,然后递归求解,是一个 ...
- Implement Trie (Prefix Tree) - LeetCode
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- Interleaving String leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- Interleaving String leetcode java
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...
- Interleaving String——Leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- reverse string | leetcode
思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...
随机推荐
- MySQL架构备份之M-S-S级联备份
M--S1--S2 级联复制 master—>slave1—>slave2 master需要开启二进制日志 中间的slave1也需要打开二进制日志,但是它默认不把应用master的操作记录 ...
- LOJ#2304 泳池
题意:有一个1001 * n的矩形,每个位置有q的概率为1.求紧贴下边界的最大的全1子矩形面积恰为k的概率.n <= 1e9,k <= 1000. 解:只需考虑每一列最下面一个0的位置. ...
- django上传图片简单验证以及自动修改图片名称
django实现文件(图片)上传之后自动修改名称以及页面上传图片时的各种提醒: 1.先在你项目中添加一个文件夹如:system 在文件夹下添加__init__.py 和storage.py文件,并在s ...
- 结构体addrinfo, sockaddr, sockaddr_in的区别
struct sockaddr和struct sockaddr_in这两个结构体用来处理网络通信的地址. 一.sockaddr sockaddr在头文件#include <sys/socket. ...
- python类的两种创建方式
参考: https://blog.csdn.net/likunkun__/article/details/81949479
- Springboot集成Thymeleaf
Thymeleaf 官方解释: Thymeleaf是一个用于web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要目的是将优雅的自然模板引入到您的开发工作流中——以使HTML可以在 ...
- Vue学习笔记五:事件修饰符
目录 什么是事件修饰符 没有事件修饰符的问题 HTML 运行 使用事件修饰符 .stop阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当事 ...
- MySQL数据转移至SQL Server详解
最近有个活是mysql数据转移到sql server 2012,直接手动转工作量太大,发现网上有工具教程,则记录一下. 一.安装MySQL ODBC驱动为MySQL安装Connector/ODBC驱动 ...
- python常用的内置函数
python常用的内置函数集合做一个归类用的时候可以查找- abs 返回数字x的绝对值或者x的摸 - all (iterable)对于可迭代的对象iterable中所有元素x都有bool(x)为tru ...
- C语言数据类型和C#的区别