给你一个由非负整数组成的数组 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
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?