Leetcode - 1010 Pairs of Songs With Total Durations Divisible by 60 (Easy)
题目描述:数组中每个元素表示歌曲的播放时间,找出所有两首歌曲时间相加之和是 60 分钟倍数的数量。
Input: [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
解题思路:60 - t % 60 的范围是 1 - 60,数组存储的范围是 0 - 59,所以要再 % 60。
public int numPairsDivisibleBy60(int[] time) {
int[] count = new int[60];
int res = 0;
for (int t : time) {
res += count[(60 - t % 60) % 60];
count[t % 60] += 1;
}
return res;
}