From 1686d980e09de5b1fa4c845af37ce528760feeee Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 4 Nov 2023 09:11:34 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0421 No.0421.Maximum XOR of Two Numbers in an Array --- .../README.md | 222 +++++++++--------- .../README_EN.md | 206 ++++++++-------- .../Solution.cpp | 91 +++---- .../Solution.go | 30 ++- .../Solution.java | 67 ++++-- .../Solution.py | 49 ++-- .../Solution.rs | 49 ++++ 7 files changed, 408 insertions(+), 306 deletions(-) create mode 100644 solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md index 9b321896c5b5d..1d3a6b1414b63 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README.md @@ -41,15 +41,15 @@ -**方法一:哈希表** - -**方法二:前缀树** +**方法一:前缀树** 题目是求两个元素的异或最大值,可以从最高位开始考虑。 我们把数组中的每个元素 $x$ 看作一个 $32$ 位的 $01$ 串,按二进制从高位到低位的顺序,插入前缀树(最低位为叶子节点)。 -搜索 $x$ 时,尽量走相反的 $01$ 字符指针的策略,因为异或运算的法则是相同得 $0$,不同得 $1$,所以我们尽可能往与 $x$ 当前位相反的字符方向走,才能得到能和 $x$ 产生最大值的结果。 +搜索 $x$ 时,尽量走相反的 $01$ 字符指针的策略,因为异或运算的法则是相同得 $0$,不同得 $1$,所以我们尽可能往与 $x$ 当前位相反的字符方向走,才能得到能和 $x$ 产生最大异或值的结果。 + +时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n \times \log M)$,其中 $n$ 是数组 $nums$ 的长度,而 $M$ 是数组中元素的最大值。 @@ -57,61 +57,40 @@ -```python -class Solution: - def findMaximumXOR(self, nums: List[int]) -> int: - max = 0 - mask = 0 - for i in range(30, -1, -1): - current = 1 << i - # 期望的二进制前缀 - mask = mask ^ current - # 在当前前缀下, 数组内的前缀位数所有情况集合 - s = set() - for num in nums: - s.add(num & mask) - # 期望最终异或值的从右数第i位为1, 再根据异或运算的特性推算假设是否成立 - flag = max | current - for prefix in s: - if prefix ^ flag in s: - max = flag - break - return max -``` - ```python class Trie: + __slots__ = ("children",) + def __init__(self): - self.children = [None] * 2 + self.children: List[Trie | None] = [None, None] - def insert(self, x): + def insert(self, x: int): node = self for i in range(30, -1, -1): - v = (x >> i) & 1 + v = x >> i & 1 if node.children[v] is None: node.children[v] = Trie() node = node.children[v] - def search(self, x): + def search(self, x: int) -> int: node = self - res = 0 + ans = 0 for i in range(30, -1, -1): - v = (x >> i) & 1 + v = x >> i & 1 if node.children[v ^ 1]: - res = res << 1 | 1 + ans |= 1 << i node = node.children[v ^ 1] else: - res <<= 1 node = node.children[v] - return res + return ans class Solution: def findMaximumXOR(self, nums: List[int]) -> int: trie = Trie() - for v in nums: - trie.insert(v) - return max(trie.search(v) for v in nums) + for x in nums: + trie.insert(x) + return max(trie.search(x) for x in nums) ``` ### **Java** @@ -119,44 +98,17 @@ class Solution: ```java -class Solution { - - public int findMaximumXOR(int[] numbers) { - int max = 0; - int mask = 0; - for (int i = 30; i >= 0; i--) { - int current = 1 << i; - // 期望的二进制前缀 - mask = mask ^ current; - // 在当前前缀下, 数组内的前缀位数所有情况集合 - Set set = new HashSet<>(); - for (int j = 0, k = numbers.length; j < k; j++) { - set.add(mask & numbers[j]); - } - // 期望最终异或值的从右数第i位为1, 再根据异或运算的特性推算假设是否成立 - int flag = max | current; - for (Integer prefix : set) { - if (set.contains(prefix ^ flag)) { - max = flag; - break; - } - } - } - return max; - } -} -``` +class Trie { + private Trie[] children = new Trie[2]; -前缀树: + public Trie() { -```java -class Trie { - Trie[] children = new Trie[2]; + } - void insert(int x) { + public void insert(int x) { Trie node = this; for (int i = 30; i >= 0; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node.children[v] == null) { node.children[v] = new Trie(); } @@ -164,20 +116,19 @@ class Trie { } } - int search(int x) { + public int search(int x) { Trie node = this; - int res = 0; + int ans = 0; for (int i = 30; i >= 0; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node.children[v ^ 1] != null) { - res = res << 1 | 1; + ans |= 1 << i; node = node.children[v ^ 1]; } else { - res <<= 1; node = node.children[v]; } } - return res; + return ans; } } @@ -185,9 +136,9 @@ class Solution { public int findMaximumXOR(int[] nums) { Trie trie = new Trie(); int ans = 0; - for (int v : nums) { - trie.insert(v); - ans = Math.max(ans, trie.search(v)); + for (int x : nums) { + trie.insert(x); + ans = Math.max(ans, trie.search(x)); } return ans; } @@ -199,34 +150,35 @@ class Solution { ```cpp class Trie { public: - vector children; - string v; + Trie* children[2]; + Trie() - : children(2) {} + : children{nullptr, nullptr} {} void insert(int x) { Trie* node = this; for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; - if (!node->children[v]) node->children[v] = new Trie(); + int v = x >> i & 1; + if (!node->children[v]) { + node->children[v] = new Trie(); + } node = node->children[v]; } } int search(int x) { Trie* node = this; - int res = 0; + int ans = 0; for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node->children[v ^ 1]) { - res = res << 1 | 1; + ans |= 1 << i; node = node->children[v ^ 1]; } else { - res <<= 1; node = node->children[v]; } } - return res; + return ans; } }; @@ -235,9 +187,9 @@ public: int findMaximumXOR(vector& nums) { Trie* trie = new Trie(); int ans = 0; - for (int v : nums) { - trie->insert(v); - ans = max(ans, trie->search(v)); + for (int x : nums) { + trie->insert(x); + ans = max(ans, trie->search(x)); } return ans; } @@ -248,17 +200,17 @@ public: ```go type Trie struct { - children [26]*Trie + children [2]*Trie } func newTrie() *Trie { return &Trie{} } -func (this *Trie) insert(x int) { - node := this +func (t *Trie) insert(x int) { + node := t for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v] == nil { node.children[v] = newTrie() } @@ -266,33 +218,85 @@ func (this *Trie) insert(x int) { } } -func (this *Trie) search(x int) int { - node := this - res := 0 +func (t *Trie) search(x int) int { + node := t + ans := 0 for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v^1] != nil { - res = res<<1 | 1 + ans |= 1 << i node = node.children[v^1] } else { - res <<= 1 node = node.children[v] } } - return res + return ans } -func findMaximumXOR(nums []int) int { +func findMaximumXOR(nums []int) (ans int) { trie := newTrie() - ans := 0 - for _, v := range nums { - trie.insert(v) - ans = max(ans, trie.search(v)) + for _, x := range nums { + trie.insert(x) + ans = max(ans, trie.search(x)) } return ans } ``` +### **Rust** + +```rust +struct Trie { + children: [Option>; 2], +} + +impl Trie { + fn new() -> Trie { + Trie { + children: [None, None], + } + } + + fn insert(&mut self, x: i32) { + let mut node = self; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if node.children[v].is_none() { + node.children[v] = Some(Box::new(Trie::new())); + } + node = node.children[v].as_mut().unwrap(); + } + } + + fn search(&self, x: i32) -> i32 { + let mut node = self; + let mut ans = 0; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if let Some(child) = &node.children[v ^ 1] { + ans |= 1 << i; + node = child.as_ref(); + } else { + node = node.children[v].as_ref().unwrap(); + } + } + ans + } +} + +impl Solution { + pub fn find_maximum_xor(nums: Vec) -> i32 { + let mut trie = Trie::new(); + let mut ans = 0; + for &x in nums.iter() { + trie.insert(x); + ans = ans.max(trie.search(x)); + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md index 0d9507fe873f3..759856904f674 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/README_EN.md @@ -36,96 +36,56 @@ ### **Python3** -```python -class Solution: - def findMaximumXOR(self, nums: List[int]) -> int: - max = 0 - mask = 0 - for i in range(30, -1, -1): - current = 1 << i - mask = mask ^ current - s = set() - for num in nums: - s.add(num & mask) - flag = max | current - for prefix in s: - if prefix ^ flag in s: - max = flag - break - return max -``` - ```python class Trie: + __slots__ = ("children",) + def __init__(self): - self.children = [None] * 2 + self.children: List[Trie | None] = [None, None] - def insert(self, x): + def insert(self, x: int): node = self for i in range(30, -1, -1): - v = (x >> i) & 1 + v = x >> i & 1 if node.children[v] is None: node.children[v] = Trie() node = node.children[v] - def search(self, x): + def search(self, x: int) -> int: node = self - res = 0 + ans = 0 for i in range(30, -1, -1): - v = (x >> i) & 1 + v = x >> i & 1 if node.children[v ^ 1]: - res = res << 1 | 1 + ans |= 1 << i node = node.children[v ^ 1] else: - res <<= 1 node = node.children[v] - return res + return ans class Solution: def findMaximumXOR(self, nums: List[int]) -> int: trie = Trie() - for v in nums: - trie.insert(v) - return max(trie.search(v) for v in nums) + for x in nums: + trie.insert(x) + return max(trie.search(x) for x in nums) ``` ### **Java** ```java -class Solution { +class Trie { + private Trie[] children = new Trie[2]; - public int findMaximumXOR(int[] numbers) { - int max = 0; - int mask = 0; - for (int i = 30; i >= 0; i--) { - int current = 1 << i; - mask = mask ^ current; - Set set = new HashSet<>(); - for (int j = 0, k = numbers.length; j < k; j++) { - set.add(mask & numbers[j]); - } - int flag = max | current; - for (Integer prefix : set) { - if (set.contains(prefix ^ flag)) { - max = flag; - break; - } - } - } - return max; - } -} -``` + public Trie() { -```java -class Trie { - Trie[] children = new Trie[2]; + } - void insert(int x) { + public void insert(int x) { Trie node = this; for (int i = 30; i >= 0; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node.children[v] == null) { node.children[v] = new Trie(); } @@ -133,20 +93,19 @@ class Trie { } } - int search(int x) { + public int search(int x) { Trie node = this; - int res = 0; + int ans = 0; for (int i = 30; i >= 0; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node.children[v ^ 1] != null) { - res = res << 1 | 1; + ans |= 1 << i; node = node.children[v ^ 1]; } else { - res <<= 1; node = node.children[v]; } } - return res; + return ans; } } @@ -154,9 +113,9 @@ class Solution { public int findMaximumXOR(int[] nums) { Trie trie = new Trie(); int ans = 0; - for (int v : nums) { - trie.insert(v); - ans = Math.max(ans, trie.search(v)); + for (int x : nums) { + trie.insert(x); + ans = Math.max(ans, trie.search(x)); } return ans; } @@ -168,34 +127,35 @@ class Solution { ```cpp class Trie { public: - vector children; - string v; + Trie* children[2]; + Trie() - : children(2) {} + : children{nullptr, nullptr} {} void insert(int x) { Trie* node = this; for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; - if (!node->children[v]) node->children[v] = new Trie(); + int v = x >> i & 1; + if (!node->children[v]) { + node->children[v] = new Trie(); + } node = node->children[v]; } } int search(int x) { Trie* node = this; - int res = 0; + int ans = 0; for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; + int v = x >> i & 1; if (node->children[v ^ 1]) { - res = res << 1 | 1; + ans |= 1 << i; node = node->children[v ^ 1]; } else { - res <<= 1; node = node->children[v]; } } - return res; + return ans; } }; @@ -204,9 +164,9 @@ public: int findMaximumXOR(vector& nums) { Trie* trie = new Trie(); int ans = 0; - for (int v : nums) { - trie->insert(v); - ans = max(ans, trie->search(v)); + for (int x : nums) { + trie->insert(x); + ans = max(ans, trie->search(x)); } return ans; } @@ -217,17 +177,17 @@ public: ```go type Trie struct { - children [26]*Trie + children [2]*Trie } func newTrie() *Trie { return &Trie{} } -func (this *Trie) insert(x int) { - node := this +func (t *Trie) insert(x int) { + node := t for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v] == nil { node.children[v] = newTrie() } @@ -235,33 +195,85 @@ func (this *Trie) insert(x int) { } } -func (this *Trie) search(x int) int { - node := this - res := 0 +func (t *Trie) search(x int) int { + node := t + ans := 0 for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v^1] != nil { - res = res<<1 | 1 + ans |= 1 << i node = node.children[v^1] } else { - res <<= 1 node = node.children[v] } } - return res + return ans } -func findMaximumXOR(nums []int) int { +func findMaximumXOR(nums []int) (ans int) { trie := newTrie() - ans := 0 - for _, v := range nums { - trie.insert(v) - ans = max(ans, trie.search(v)) + for _, x := range nums { + trie.insert(x) + ans = max(ans, trie.search(x)) } return ans } ``` +### **Rust** + +```rust +struct Trie { + children: [Option>; 2], +} + +impl Trie { + fn new() -> Trie { + Trie { + children: [None, None], + } + } + + fn insert(&mut self, x: i32) { + let mut node = self; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if node.children[v].is_none() { + node.children[v] = Some(Box::new(Trie::new())); + } + node = node.children[v].as_mut().unwrap(); + } + } + + fn search(&self, x: i32) -> i32 { + let mut node = self; + let mut ans = 0; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if let Some(child) = &node.children[v ^ 1] { + ans |= 1 << i; + node = child.as_ref(); + } else { + node = node.children[v].as_ref().unwrap(); + } + } + ans + } +} + +impl Solution { + pub fn find_maximum_xor(nums: Vec) -> i32 { + let mut trie = Trie::new(); + let mut ans = 0; + for &x in nums.iter() { + trie.insert(x); + ans = ans.max(trie.search(x)); + } + ans + } +} +``` + ### **...** ``` diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp index 7e7ce4ab975f1..d5deae45089be 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp @@ -1,45 +1,46 @@ -class Trie { -public: - vector children; - string v; - Trie() - : children(2) {} - - void insert(int x) { - Trie* node = this; - for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; - if (!node->children[v]) node->children[v] = new Trie(); - node = node->children[v]; - } - } - - int search(int x) { - Trie* node = this; - int res = 0; - for (int i = 30; ~i; --i) { - int v = (x >> i) & 1; - if (node->children[v ^ 1]) { - res = res << 1 | 1; - node = node->children[v ^ 1]; - } else { - res <<= 1; - node = node->children[v]; - } - } - return res; - } -}; - -class Solution { -public: - int findMaximumXOR(vector& nums) { - Trie* trie = new Trie(); - int ans = 0; - for (int v : nums) { - trie->insert(v); - ans = max(ans, trie->search(v)); - } - return ans; - } -}; \ No newline at end of file +class Trie { +public: + Trie* children[2]; + + Trie() + : children{nullptr, nullptr} {} + + void insert(int x) { + Trie* node = this; + for (int i = 30; ~i; --i) { + int v = x >> i & 1; + if (!node->children[v]) { + node->children[v] = new Trie(); + } + node = node->children[v]; + } + } + + int search(int x) { + Trie* node = this; + int ans = 0; + for (int i = 30; ~i; --i) { + int v = x >> i & 1; + if (node->children[v ^ 1]) { + ans |= 1 << i; + node = node->children[v ^ 1]; + } else { + node = node->children[v]; + } + } + return ans; + } +}; + +class Solution { +public: + int findMaximumXOR(vector& nums) { + Trie* trie = new Trie(); + int ans = 0; + for (int x : nums) { + trie->insert(x); + ans = max(ans, trie->search(x)); + } + return ans; + } +}; diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.go b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.go index e7b02dc1db0fa..3ddfa72386570 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.go +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.go @@ -1,15 +1,15 @@ type Trie struct { - children [26]*Trie + children [2]*Trie } func newTrie() *Trie { return &Trie{} } -func (this *Trie) insert(x int) { - node := this +func (t *Trie) insert(x int) { + node := t for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v] == nil { node.children[v] = newTrie() } @@ -17,28 +17,26 @@ func (this *Trie) insert(x int) { } } -func (this *Trie) search(x int) int { - node := this - res := 0 +func (t *Trie) search(x int) int { + node := t + ans := 0 for i := 30; i >= 0; i-- { - v := (x >> i) & 1 + v := x >> i & 1 if node.children[v^1] != nil { - res = res<<1 | 1 + ans |= 1 << i node = node.children[v^1] } else { - res <<= 1 node = node.children[v] } } - return res + return ans } -func findMaximumXOR(nums []int) int { +func findMaximumXOR(nums []int) (ans int) { trie := newTrie() - ans := 0 - for _, v := range nums { - trie.insert(v) - ans = max(ans, trie.search(v)) + for _, x := range nums { + trie.insert(x) + ans = max(ans, trie.search(x)) } return ans } \ No newline at end of file diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.java b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.java index 6b556b1def977..b8cdd79995b12 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.java +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.java @@ -1,23 +1,44 @@ -class Solution { - - public int findMaximumXOR(int[] numbers) { - int max = 0; - int mask = 0; - for (int i = 30; i >= 0; i--) { - int current = 1 << i; - mask = mask ^ current; - Set set = new HashSet<>(); - for (int j = 0, k = numbers.length; j < k; j++) { - set.add(mask & numbers[j]); - } - int flag = max | current; - for (Integer prefix : set) { - if (set.contains(prefix ^ flag)) { - max = flag; - break; - } - } - } - return max; - } -} +class Trie { + private Trie[] children = new Trie[2]; + + public Trie() { + } + + public void insert(int x) { + Trie node = this; + for (int i = 30; i >= 0; --i) { + int v = x >> i & 1; + if (node.children[v] == null) { + node.children[v] = new Trie(); + } + node = node.children[v]; + } + } + + public int search(int x) { + Trie node = this; + int ans = 0; + for (int i = 30; i >= 0; --i) { + int v = x >> i & 1; + if (node.children[v ^ 1] != null) { + ans |= 1 << i; + node = node.children[v ^ 1]; + } else { + node = node.children[v]; + } + } + return ans; + } +} + +class Solution { + public int findMaximumXOR(int[] nums) { + Trie trie = new Trie(); + int ans = 0; + for (int x : nums) { + trie.insert(x); + ans = Math.max(ans, trie.search(x)); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.py b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.py index 316c113b0e4de..a8ab524ced0f9 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.py +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.py @@ -1,16 +1,33 @@ -class Solution: - def findMaximumXOR(self, nums: List[int]) -> int: - max = 0 - mask = 0 - for i in range(30, -1, -1): - current = 1 << i - mask = mask ^ current - s = set() - for num in nums: - s.add(num & mask) - flag = max | current - for prefix in s: - if prefix ^ flag in s: - max = flag - break - return max +class Trie: + __slots__ = ("children",) + + def __init__(self): + self.children: List[Trie | None] = [None, None] + + def insert(self, x: int): + node = self + for i in range(30, -1, -1): + v = x >> i & 1 + if node.children[v] is None: + node.children[v] = Trie() + node = node.children[v] + + def search(self, x: int) -> int: + node = self + ans = 0 + for i in range(30, -1, -1): + v = x >> i & 1 + if node.children[v ^ 1]: + ans |= 1 << i + node = node.children[v ^ 1] + else: + node = node.children[v] + return ans + + +class Solution: + def findMaximumXOR(self, nums: List[int]) -> int: + trie = Trie() + for x in nums: + trie.insert(x) + return max(trie.search(x) for x in nums) diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs new file mode 100644 index 0000000000000..6e48796b5c8fe --- /dev/null +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.rs @@ -0,0 +1,49 @@ +struct Trie { + children: [Option>; 2], +} + +impl Trie { + fn new() -> Trie { + Trie { + children: [None, None], + } + } + + fn insert(&mut self, x: i32) { + let mut node = self; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if node.children[v].is_none() { + node.children[v] = Some(Box::new(Trie::new())); + } + node = node.children[v].as_mut().unwrap(); + } + } + + fn search(&self, x: i32) -> i32 { + let mut node = self; + let mut ans = 0; + for i in (0..=30).rev() { + let v = (x >> i & 1) as usize; + if let Some(child) = &node.children[v ^ 1] { + ans |= 1 << i; + node = child.as_ref(); + } else { + node = node.children[v].as_ref().unwrap(); + } + } + ans + } +} + +impl Solution { + pub fn find_maximum_xor(nums: Vec) -> i32 { + let mut trie = Trie::new(); + let mut ans = 0; + for &x in nums.iter() { + trie.insert(x); + ans = ans.max(trie.search(x)); + } + ans + } +} \ No newline at end of file