lc
lc2964
枚举中间+hash
class Solution {
public:
int divisibleTripletCount(vector<int>& nums, int d)
{
int n = nums.size();
if (n < 3) return 0;
int ret = 0;
unordered_map<int, int> hash;
hash[nums[0] % d]++;
// 枚举中间
for (int i = 1; i < n - 1; ++i) {
int mod_i = nums[i] % d;
// 枚举后面
for (int j = i + 1; j < n; ++j) {
int mod_j = nums[j] % d;
int t = (d - (mod_i + mod_j) % d) % d;
ret += hash[t];
}
hash[mod_i]++;
}
return ret;
}
};