leetcode 困难题 1803. Count Pairs With XOR in a Range
2026/9/14 19:41:54 网站建设 项目流程

Problem: 1803. 统计异或值在范围内的数对有多少

统计频次的,然后排序,计算的时候剪枝,a + hh < low的跳过,若 hh - a > high也需要跳过,hh + a是 hh ^ a 的最大值, hh - a是最小值

Code

class Solution { public: vector<int> ump; int countPairs(vector<int>& nums, int low, int high) { int a, hh, c, sum = 0; ump.assign(20001, 0); for(int& i : nums) ump[i]++; nums.clear(); for(int i = 0; i < 20001; i++) { if(ump[i] > 0) nums.push_back(i); } sort(nums.begin(), nums.end()); int n = nums.size(); for(int i = 0; i < n; i++) { a = nums[i]; for(int j = i + 1; j < n; j++) { hh = nums[j]; if((a | hh) < low) continue; c = (a ^ hh); if( c >= low && c <= high) sum += ump[a] * ump[hh]; if(hh - a > high) break; } } return sum; } };

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询