classTrie{public:
vector<Trie*> next;Trie():next(2,nullptr){}voidinsert(int num){
Trie* cur =this;for(int i =31; i >=0;--i){int idx =(num >> i)&1;if(!cur->next[idx]) cur->next[idx]=newTrie();
cur = cur->next[idx];}return;}intsearchXOR(int num){int tmp = num;
Trie* cur =this;int ans =0;for(int i =31; i >=0;--i){int idx =(num >> i)&1;int ridx =1-idx;// 另一方向if(cur->next[ridx])// 优先向另一方向走{
ans +=(1<< i);
cur = cur->next[ridx];}else
cur = cur->next[idx];}return ans;}};classSolution{private:
Trie* root;public:intfindMaximumXOR(vector<int>& nums){
root =newTrie();for(int num : nums) root->insert(num);int ans =0;for(int num : nums) ans =max(ans, root->searchXOR(num));return ans;}};