lc3128
枚举中间+乘法原理
class Solution {
public:
long long numberOfRightTriangles(vector<vector<int>>& grid) {
int n = grid[0].size();
vector<int> col_sum(n, -1); // 提前减一
for (auto& row : grid) {
for (int j = 0; j < n; j++) {
col_sum[j] += row[j];
}
}
long long ans = 0;
for (auto& row : grid) {
int row_sum = reduce(row.begin(), row.end()) - 1; // 提前减一
for (int j = 0; j < row.size(); j++) {
if (row[j] == 1) {
ans += row_sum * col_sum[j];
}
}
}
return ans;
}
};
lcr67
set筛集合 反复询问下一位是否可为1
贪心+hash,从最高位到最低位逐位确定最大异或值
掩码保留高位,避免影响判位、异或配对验证

class Solution {
public:
int findMaximumXOR(vector<int>& nums)
{
int high_bit = __lg(ranges::max(nums));
int ans = 0, mask = 0;
unordered_set<int> seen;
for (int i = high_bit; i >= 0; i--) {
seen.clear();
mask |= 1 << i;
int new_ans = ans | (1 << i);
// 这个比特位可以是 1 吗?
for (int x : nums) {
x &= mask; // 低于 i 的比特位置为 0
if (seen.contains(new_ans ^ x)) {
ans = new_ans;
// 这个比特位可以是 1
break;
}
++seen.insert(x);++
}
}
return ans;
}
};