题目:
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: 28

Explanation: The maximum result is 5 ^ 25 = 28

代码:

#include<iostream>
#include<set>
using namespace std;

int findMaximumXOR(int *nums,int a) {
set<int>::iterator it1;
  for(int i=0; i<6; i++){
cout<<nums[i]<<endl;
}
        int max = 0, mask = 0;
        for(int i = 31; i >= 0; i--){
            mask = mask | (1 << i);
            set<int> set1;
            for(int j=0; j<6; j++){
            // cout<<"nums[i]="<<nums[i]<<"; mask="<<mask<<"; nums[i]&mask="<<(nums[i]&mask)<<endl;
                set1.insert(nums[j] & mask);
            }
            cout<<"bianli start;"<<endl;
            for (it1 = set1.begin(); it1 != set1.end(); ++it1)    
   {
        cout << *it1 << endl;
    }
    cout<<"bianli end;"<<endl;
            int tmp = max | (1 << i);
            for(set<int>::iterator it = set1.begin(); it != set1.end() ;it++){
                if(set1.end()!=set1.find(tmp ^ (*it))) {
                cout<<"tmp="<<tmp<<"; tmp^(*it)="<<(tmp^(*it))<<"; *it="<<(*it)<<endl;
                    max = tmp;
                    break;
                }
            }
            cout<<endl;
        }
        return max;
    };

int main(){

int nums[] = {
2,3,5,10,8,25
};
int max = findMaximumXOR(nums,6);
cout<<max;
}
解释:通过mask从左往右和nums中的数&,mask从左往右依次增加1位。
set1中的数据为nums中的数分别与mask &之后的值,即nums中的数被从左往右依次处理。
tmp为当前max和新的一位或运算,然后通过下面的循环判断tmp是否为合适的max
如果tmp能与set1中的某一个数异或后还存在于set1中的话,那么set1中的这两个相关数异或后就可以成为tmp,即当前最大值。然后就可以把它赋值给max。
这里用了异或的自反性,即 a ^ b = c,而a ^ b ^ b = a, 则 c ^ b = a。理解这一点很重要。
说明:我的代码只是用于例子,不具有通用性,需要改一改。

421. Maximum XOR of Two Numbers in an Array详解相关推荐

  1. [leetcode]421. Maximum XOR of Two Numbers in an Array

    421. Maximum XOR of Two Numbers in an Array Given a non-empty array of numbers, a0, a1, a2, - , a

  2. 421. Maximum XOR of Two Numbers in an Array——本质:利用trie数据结构查找

    Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  3. leetcode 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 re ...

  4. 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 re ...

  5. leetcode 421. Maximum XOR of Two Numbers in an Array | 421. 数组中两个数的最大异或值(位运算,Trie前缀树)

    题目 https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/ 题解 自己没有思路,看了答案之后自己写的. 参考:Py ...

  6. 421. Maximum XOR of Two Numbers in an Array [Medium]

    自己不会做,连解答都看的比较吃力 对位运算还是不太熟悉,常见技巧在位运算场景中容易用不出来 一姐讲解视频: https://www.bilibili.com/video/BV1pr4y1c7B8?fr ...

  7. LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法

    LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...

  8. 【LeetCode-421】Maximum XOR of Two Numbers in an Array

    Given a list of numbers, a[0], a[1], a[2], - , a[N-1], where 0 <= a[i] < 2^32. Find the maximu ...

  9. LeetCode Maximum XOR of Two Numbers in an Array(贪心、字典树)

    问题:给出一个非空的整数数组,找到最大的两个数的异或值.要求时间复杂度为O(n) 思路:根据二进制表示的前缀. 先求出最大数的位数,然后根据位数作位操作异或判断最大值.由于要求时间复杂度为O(n),在 ...

最新文章

  1. retinaface tensorRT
  2. 数据库SQL的分组函数
  3. mysql 备库 hang住_mysql主键的缺少导致备库hang住
  4. 小型数控雕刻机制作Arduino_开一家全屋定制装修公司,怎么选择开料机与雕刻机?...
  5. Linux nano编辑txt文件,Linux 文本编辑器 nano 的简单使用
  6. 【WP8.1开发】RenderTargetBitmap类的特殊用途
  7. Java基础夺命连环16问
  8. github上传_上传本地代码到github
  9. 你最缺钱的时候是怎么度过的?
  10. MySQL 时间戳转换成秒
  11. 服务器做raid bios界面做raid配置
  12. Windows8 10设置程序为 系统默认浏览器
  13. java编辑双层pdf文件,双层PDF制作系统
  14. 从我的历程谈谈该如何学习
  15. scipy--统计检验
  16. FFmpeg一些感想
  17. 协议分析(微信网页版 wx2.qq.com)
  18. 中国人工智能领域企业分类(附未来企业排行)
  19. 现实赢了袖手旁观他在冷眼看我们
  20. 删除腾讯电脑管家的卸载残余qmbsrv

热门文章

  1. 【学习教程系列】最通俗的 Python3 网络爬虫入门
  2. 最近很火的农民工,每一句话都在提醒舒适区的我们
  3. 不等金九银十,金风八月,我早已拿下字节跳动的offer
  4. 微信打开链接可以实现微信跳转手机浏览器,实现微信下载APP或者打开指定链接。
  5. vue 免费的每天不限次数的调用天气接口
  6. 这15种面相的人值得深交
  7. putty删除键backspace设置
  8. k均值聚类算法伪代码
  9. linux笔记实录(2)
  10. 攻防世界—MISC 新手区1-12