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的更多相关文章

  1. Implement Trie (Prefix Tree) ——LeetCode

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  2. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  3. Scramble String -- LeetCode

    原题链接: http://oj.leetcode.com/problems/scramble-string/  这道题看起来是比較复杂的,假设用brute force,每次做分割,然后递归求解,是一个 ...

  4. Implement Trie (Prefix Tree) - LeetCode

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  5. 438. Find All Anagrams in a String - LeetCode

    Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...

  6. Interleaving String leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  7. Interleaving String leetcode java

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given ...

  8. Interleaving String——Leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  9. reverse string | leetcode

    思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...

随机推荐

  1. Eureka 配置

    #是否向服务注册中心注册自己,该值默认为trueeureka.client.register-with-eureka=falseserver端建议设为false #服务注册中心的配置内容,指定服务注册 ...

  2. SAM求多个串的最长公共子串

    又学到一个\(SAM\)的新套路QvQ 思路 考虑用其中的一个串建个\(SAM\),然后用其他的串在上面匹配,匹配时更新答案 首先有一个全局变量\(len\),表示当前已匹配的长度.假设目前在点\(u ...

  3. postgresql某个字段值按照指定规则排序

    select id,serial_group_id,state from ap_model order by serial_group_id asc, ( case when state=1 then ...

  4. Codeforces Round #554 (Div. 2) B. Neko Performs Cat Furrier Transform(思维题+log2求解二进制位数的小技巧)

    传送门 题意: 给出一个数x,有两个操作: ①:x ^= 2k-1; ②:x++; 每次操作都是从①开始,紧接着是② ①②操作循环进行,问经过多少步操作后,x可以变为2p-1的格式? 最多操作40次, ...

  5. JavaWeb之DButils整理

    一.DBUtils介绍  apache 什么是dbutils,它的作用 DBUtils是java编程中的数据库操作实用工具,小巧简单实用. 用前导包!!!DBUtils包!!! 二.DBUtils的三 ...

  6. 编写高质量的Python代码系列(四)之元类及属性

    元类(metaclass)及动态属性(dynamic attribute)都是很强大的Python特性,然后他们也可能导致及其古怪.及其突然的行为.本节讲解这些机制的常见用法,以确保各位程序员写出来的 ...

  7. OAuth2

    OAuth2: 适合To C的应用场景, 比如我们开发一个app, 可以借用微信/微博用户认证开放接口, 达到免注册登陆, 企业内部系统没有必要引入. OAuth2的步骤较多, 角色也较多, 涉及到a ...

  8. 二叉树(BT)相关

    1.same tree /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * ...

  9. JDBC——连接数据库

    JDBC的基本介绍 1.概述:jdbc是使用Java访问各种数据库的一种技术 (1)jdbc工作原理 2.jdbc核心Java类(API) (1)DriverManager类 作用:管理各种数据库的驱 ...

  10. LeetCode刷题-005最长回文子串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000.示例 1:输入: "babad"输出: "bab"注意: "ab ...