chenfy27的刷题记录

导航

leetcode421 数组中两个数的最大异或值

给定数组nums[n],求nums[i]^nums[j]的最大值,其中0<=i<=j<n。
1<=n<=2E5; 0<=nums[i]<2^31

分析:01trie模板题。

// 01trie模板。。。

class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        Trie tr;
        tr.init(INT_MAX);
        int ans = 0;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            tr.insert(nums[i]);
            ans = std::max(ans, nums[i] ^ tr.find(nums[i]));
        }
        return ans;
    }
};

posted on 2024-12-29 15:01  chenfy27  阅读(45)  评论(0)    收藏  举报