原题链接:https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/
1、方法一:暴力法

func findMaximumXOR(nums []int) int {res := 0for i := 1; i < len(nums); i++ {for j := 0; j < i; j++ {res = max(res, nums[i] ^ nums[j])}}return res
}
func max(a int, b int) int {if a > b {return a} else {return b}
}

2、方法二:二进制前缀树

type tree struct {left, right *tree
}func findMaximumXOR(nums []int) int {L := 30root := &tree{}for _, num := range nums {curr := rootfor j := L; j >= 0; j-- {v := (num >> j) & 1if v == 0 {if curr.left == nil {curr.left = &tree{}}curr = curr.left} else {if curr.right == nil {curr.right = &tree{}}curr = curr.right}}}res := 0for _, num := range nums {curr := roottotal := 0for i := L; i >= 0; i-- {v := (num >> i) & 1if v == 0 {if curr.right != nil {curr = curr.righttotal = total * 2 + 1} else {curr = curr.lefttotal = total * 2}} else {if curr.left != nil {curr = curr.lefttotal = total * 2 + 1} else {curr = curr.righttotal = total * 2}}}res = max(res, total)}return res
}func max(a, b int) int {if a > b {return a}return b
}

leetcode算法题--数组中两个数的最大异或值相关推荐

  1. Leetcode :421. 数组中两个数的最大异或值 (位运算)

    421. 数组中两个数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算 ...

  2. LeetCode 421. 数组中两个数的最大异或值

    421. 数组中两个数的最大异或值 Idea 假设选择了数组中的元素ai和aj使得它们达到最大按位异或结果x:x=ai⊕ajx=a_{i}⊕a_{j}x=ai​⊕aj​,其中⊕表示按位异或运算. 根据 ...

  3. arrays中copyof复制两个数组_异或的魅力!图解「数组中两个数的最大异或值」

    今天分享的题目来源于 LeetCode 第 421 号问题:数组中两个数的最大异或值.在 异或 这个知识点里面属于一个中高难度的题目. 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, ...

  4. 数组中两个数的最大异或值 两数异或值一定小于两数相加和

    链接:数组中两个数的最大异或值 给你一个整数数组 nums ,返回 nums[i] XOR nums[j] 的最大运算结果,其中 0 ≤ i ≤ j < n . 进阶:你可以在 O(n) 的时间 ...

  5. 421. 数组中两个数的最大异或值

    题目:421. 数组中两个数的最大异或值 题解:https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/solu ...

  6. leetcode421. 数组中两个数的最大异或值(贪心算法)

    给你一个整数数组 nums ,返回 nums[i] XOR nums[j] 的最大运算结果,其中 0 ≤ i ≤ j < n . 进阶:你可以在 O(n) 的时间解决这个问题吗? 示例 1: 输 ...

  7. LeetCode 421. 数组中两个数的最大异或值(Trie树)

    1. 题目 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, ...

  8. 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 ...

  9. leetcode算法题--数组中数字出现的次数 II

    原题链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/ int singleNumb ...

最新文章

  1. Visual Studio2005入门.Net系列视频教程
  2. SAP MM模块的预留详解
  3. 回味jQuery系列(1)-选择器
  4. 盘点中国互联网行业10年2万多起投融资,17年投融资形势走向何处
  5. 分支和循环_月隐学python第5课
  6. 论文公式编号右对齐_论文不会写?最详细的论文排版技巧
  7. vs2019创建android,Visual Studio 2019编写的安卓程序,生成失败
  8. Linux命令执行顺序
  9. 图文详解!java高级工程师简历模板
  10. Primavera P6用户密码锁定及管理员忘记密码处理
  11. EPS电动转向系统分析
  12. windows怎样运行wmi服务器,windows系统wmi服务器
  13. xp桌面计算机隐藏设置密码,电脑设置开机密码详解 保护自己的隐私不受窥探-电脑设置开机密码...
  14. 百度云离线下载含有违规内容检測方法分析
  15. 【计算机系统结构】第3章 流水线技术问答题
  16. HNU 12814 SIRO Challenge(最短路+状态压缩+dp)
  17. 214 情人节来袭,电视剧 《点燃我温暖你》李峋同款 Python爱心表白代码,赶紧拿去用吧
  18. 来自不同行业领域的50多个对象检测数据集
  19. 电源拓扑结构优缺点比较-常见开关电源优缺点对比
  20. 兄弟连区块链入门教程分享区块链POW证明代码实现demo

热门文章

  1. python装饰器-如何更通俗地讲解Python的装饰器?
  2. python知乎-学会python的好处,轻易搭建自己的知乎
  3. python爬百度翻译-Python爬取百度翻译(利用json提取数据)
  4. python上海培训哪里比较好-上海培训python一般多少钱?
  5. python编程有什么用处-python中的生成器是什么?生成器有什么用处?
  6. python第三方库是什么意思-Python最强大的第三方库,你有必要了解一下!
  7. python银行系统-python银行系统实现源码
  8. python基础题-python基础练习题(一)
  9. 计算机编程语言python-PYTHON之计算机语言基础知识 —— 编程语言的分类
  10. python中怎么安装pip-python中怎么安装pip