给你一个由非负整数组成的数组 nums 。另有一个查询数组 queries ,其中 queries[i] = [xi, mi] 。
第 i 个查询的答案是 xi 和任何 nums 数组中不超过 mi 的元素按位异或(XOR)得到的最大值。换句话说,答案是 max(nums[j] XOR xi) ,其中所有 j 均满足 nums[j] = 0; i--){
int flag = (n >> i) & 1;
if(t->child[flag] == nullptr){
t->child[flag] = new Trie();
}
t = t->child[flag];
}
}
int getMaxXor(int n){
int result = 0;
Trie * t = this;
for(int i = length; i >= 0; i--){
int flag = (n >> i) & 1;
if(t->child[flag ^ 1] != nullptr){
result |= 1 isWord = true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
Trie * t = this;
for(char c: word){
if(!t->child[c-'a']){
return false;
}
else{
t = t->child[c-'a'];
}
}
return t->isWord;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
Trie *t = this;
for(char c:prefix){
if(!t->child[c-'a']){
return false;
}
t = t->child[c - 'a'];
}
return true;
}
};
/**
* 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);
*/
2、C++中的空指针为nullptr
3、复合位运算
n的第i加权位是否为1:(n>>i) ^ 1
0,1互转:i ^= 1
将第i个加权位置为1:n |= (1
- 【Leetcode】剑指Offer 34:二叉树中和为某一值的路径
- 【Leetcode】剑指Offer 33:二叉搜索树的后序遍历序列
- 【Leetcode】剑指Offer 32-III: 从上到下打印二叉树 III
- 【Leetcode】剑指Offer 32-II: 从上到下打印二叉树 II
- 【Leetcode】剑指Offer 32-I:从上到下打印二叉树
- 【Leetcode】剑指Offer 31:栈的压入、弹出序列
- 【Leetcode】剑指Offer 30:包含min函数的栈
- 【Leetcode】剑指Offer 29:顺时针打印矩阵
- 【Leetcode】剑指Offer 28:对称的二叉树
- 【Leetcode】剑指Offer 27:二叉树的镜像
