您当前的位置: 首页 > 

孑渡

暂无认证

  • 0浏览

    0关注

    178博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

每日一题2020/5/23(好题):与数组中元素的最大异或值

孑渡 发布时间:2021-05-23 11:29:23 ,浏览量:0

给你一个由非负整数组成的数组 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

关注
打赏
1663211900
查看更多评论
立即登录/注册

微信扫码登录

0.0378s