Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int findMaximumXOR(int[] nums) {
- if(nums == null || nums.length < 2) return 0;
- Node r = new Node();
- int max = nums[0];
- for(int k : nums)
- {
- Node root = r;
- max = Math.max(max, k);
- for(int i = 31; i >= 0; i--)
- {
- if((k & (1 << i)) != 0)
- {
- if(root.left == null) root.left = new Node();
- root = root.left;
- }
- else
- {
- if(root.right == null) root.right = new Node();
- root = root.right;
- }
- }
- }
- int xor = 0;
- for(int k : nums)
- {
- Node root = r;
- int curxor = 0;
- for(int i = 31; i >= 0; i--)
- {
- curxor <<= 1;
- if((k & (1 << i)) != 0)
- {
- if(root.right != null) root = root.right;
- else
- {
- root = root.left;
- curxor |= 1;
- }
- }
- else
- {
- if(root.left != null)
- {
- root = root.left;
- curxor |= 1;
- }
- else root = root.right;
- }
- }
- xor = Math.max(xor, k ^ curxor);
- }
- return xor;
- }
- }
- class Node
- {
- Node left, right;
- }
Add Comment
Please, Sign In to add comment