题目:
题解:
class Solution {// 字典树的根节点Trie root = new Trie();// 最高位的二进制位编号为 30static final int HIGH_BIT = 30;public int findMaximumXOR(int[] nums) {int n = nums.length;int x = 0;for (int i = 1; i < n; ++i) {// 将 nums[i-1] 放入字典树,此时 nums[0 .. i-1] 都在字典树中add(nums[i - 1]);// 将 nums[i] 看作 ai,找出最大的 x 更新答案x = Math.max(x, check(nums[i]));}return x;}public void add(int num) {Trie cur = root;for (int k = HIGH_BIT; k >= 0; --k) {int bit = (num >> k) & 1;if (bit == 0) {if (cur.left == null) {cur.left = new Trie();}cur = cur.left;}else {if (cur.right == null) {cur.right = new Trie();}cur = cur.right;}}}public int check(int num) {Trie cur = root;int x = 0;for (int k = HIGH_BIT; k >= 0; --k) {int bit = (num >> k) & 1;if (bit == 0) {// a_i 的第 k 个二进制位为 0,应当往表示 1 的子节点 right 走if (cur.right != null) {cur = cur.right;x = x * 2 + 1;} else {cur = cur.left;x = x * 2;}} else {// a_i 的第 k 个二进制位为 1,应当往表示 0 的子节点 left 走if (cur.left != null) {cur = cur.left;x = x * 2 + 1;} else {cur = cur.right;x = x * 2;}}}return x;}
}class Trie {// 左子树指向表示 0 的子节点Trie left = null;// 右子树指向表示 1 的子节点Trie right = null;
}