算法 总持续时间可被 60 整除的歌曲 求解代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public int numPairsDivisibleBy60(int[] time) { // 创建长度为60的数组,下标对应余数0~59,存储对应余数出现的次数 int[] arr = new int[60]; int ans = 0; for (int t : time) { // 计算当前时长对60取余,将数值范围压缩到 0~59 t %= 60; //查找能和当前余数t配对的余数的出现次数,累加到结果中 ans += arr[(60 - t) % 60]; // 将当前余数的计数+1,存入数组,供后续元素配对使用 arr[t]++; } return ans; }