[leetcode]421. Maximum XOR of Two Numbers in an Array
来源:互联网 发布:哈布斯堡的灭亡知乎 编辑:程序博客网 时间:2024/05/16 02:44
421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
Could you do this in O(n) runtime?
Example:
Input: [3, 10, 5, 25, 2, 8]Output: 28Explanation: The maximum result is 5 ^ 25 = 28.
思路一:
利用a ^ b = c,而a ^ b ^ b = a, 则 c ^ b = a.
int findMaximumXOR(vector<int>& nums) { if( nums.size() < 2 ) return 0; int maxNum = 0; int flag = 0; for( int i = 31; i >= 0; --i ) {//<span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">从最大值maxNum最高位到最低位开始确定</span> set<int> hash; flag |= (1<<i); for( int x: nums ) { hash.insert( flag & x ); } int tmp = maxNum | ( 1<<i ); for( int x: hash ) { if( hash.find( x ^ tmp ) != hash.end() ) { maxNum = tmp; break; } } } return maxNum; }
思路二:
Tire树,利用nums中的数构建tire树,然后依次查找数组nums中数的异或最大值
struct Node{ Node * next[2]; Node() { next[0] = nullptr; next[1] = nullptr; } }; void buildTireTree(Node* root, int x) { for( int i = 31; i >= 0; --i ) { int flag = ( x & (1<<i) ) ? 1 : 0; if( root->next[flag] == nullptr ) { root->next[flag] = new Node(); } root = root->next[flag]; } } int findMaxXorInTire(Node* root, int x) { int result = 0; for( int i = 31; i >= 0; --i ) { int flag = ( x & ( 1<<i) )? 0: 1; if( root->next[flag] != nullptr ) { result |= (1<<i); root = root->next[flag]; } else root = root->next[1-flag]; } return result; } public: int findMaximumXOR(vector<int>& nums) { if( nums.size() < 2 ) return 0; Node head; for( int x: nums ) { buildTireTree( &head, x ); } int maxNum = 0;//INT_MIN; for( int x: nums ) { int m = findMaxXorInTire( &head, x ); maxNum = max( maxNum, m ); } return maxNum; }
0 0
- [leetcode]421. Maximum XOR of Two Numbers in an Array
- 【LeetCode】 421. Maximum XOR of Two Numbers in an Array
- [leetcode]421. Maximum XOR of Two Numbers in an Array
- Leetcode-421. Maximum XOR of Two Numbers in an Array
- [LeetCode]421. Maximum XOR of Two Numbers in an Array
- leetcode 421. Maximum XOR of Two Numbers in an Array
- (LeetCode) 421. Maximum XOR of Two Numbers in an Array
- leetcode 421. Maximum XOR of Two Numbers in an Array
- LeetCode: 421. Maximum XOR of Two Numbers in an Array
- leetcode 421. Maximum XOR of Two Numbers in an Array
- 【LeetCode-421】Maximum XOR of Two Numbers in an Array
- LeetCode #421: Maximum XOR of Two Numbers in an Array
- Leetcode Maximum XOR of Two Numbers in an Array
- 【LeetCode】Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array详解
- 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- 421. Maximum XOR of Two Numbers in an Array
- linux串口权限问题
- ES6学习18(二进制数组)
- 总结js常用函数和常用技巧(持续更新)
- Reporting Service 运行报错,提示解密对称密钥出现错误
- java序列化
- [leetcode]421. Maximum XOR of Two Numbers in an Array
- 一个不起眼的公告
- 初学字符串
- openssl pem文件的读取
- 评论页面表情处理(emoji)
- LRN层的实现
- Android 拍照和从相册选照片
- 记一次有趣的社区活动
- 微信写日志