class Solution {
public int numPairsDivisibleBy60(int[] time) {
int count = 0;
int[] seconds = new int[60];
for(int t : time) {
seconds[t % 60] += 1;
}
count += (seconds[0]*(seconds[0]-1)/2+seconds[30]*(seconds[30]-1)/2);
int i = 1, j = 59;
while(i < j) {
count += seconds[i++] * seconds[j--];
}
return count;
}
}其实用hash的思想我已经想到了,但是没有注意到它的特殊性,也就是组合方式的多样性,余数为0和余数为30的需要特殊考虑,也就是各自K*(K-1)/2,然后其他的从1-29对应到31-59都是m乘n的计数方式。
这其实是一道组合数字的题目。

京公网安备 11010502036488号