/** * @param {number[]} nums 0 <= nums[i] <= 2 ^ 31 - 1 * @return {number} */ var findMaximumXOR = function (nums) { let heightBit = 30; let x = 0; // 取高位 for (let k = heightBit; k >= 0; k -= 1) { // 假设能取到1 x = (x << 1) + 1; let found = false;
// 双循环判断是否能取到1 for (let i = 0; i < nums.length; i += 1) { for (let j = i + 1; j < nums.length; j += 1) { if (((nums[i] >> k) ^ (nums[j] >> k)) == x) { found = true; break; } } }
/** * @param {number[]} nums 0 <= nums[i] <= 2 ^ 31 - 1 * @return {number} */ var findMaximumXOR = function (nums) { // 碰碰运气 if (nums.length <= 1) return0; if (nums.length == 2) return nums[0] ^ nums[1]; constHEIGHT_BIT = 30; let x = 0; for (let k = HEIGHT_BIT; k >= 0; k -= 1) { let set = newSet(); for (let i = 0; i < nums.length; i += 1) { set.add(nums[i] >> k); }
x = x * 2 + 1; let found = false;
for (let i = 0; i < nums.length; i += 1) { if (set.has(x ^ (nums[i] >> k))) { found = true; break; } }
let trie = newTrie(); for (let i = 0; i < nums.length; i += 1) { let num = nums[i].toString(2).padStart(HEIGHT_BIT + 1, "0"); trie.insert(num); }
let x = 0; for (let k = HEIGHT_BIT; k >= 0; k -= 1) { let found = false; x = x * 2 + 1; for (let i = 0; i < nums.length; i += 1) { let prefix = (x ^ (nums[i] >> k)) .toString(2) .padStart(HEIGHT_BIT - k + 1, "0"); if (trie.startsWith(prefix)) { found = true; break; } }