1010.总持续时间可被60整除的歌曲
链接:1010.总持续时间可被60整除的歌曲
难度:Medium
标签:数组、哈希表、计数
简介:返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。
题解 1 - cpp
- 编辑时间:2023-05-07
- 执行用时:28ms
- 内存消耗:27.5MB
- 编程语言:cpp
- 解法介绍:取模后求逆元。
class Solution {
public:
    int numPairsDivisibleBy60(vector<int>& time) {
        unordered_map<int, int> m;
        int res = 0;
        for (auto &t : time) {
            if (t % 60 == 0) res += m[0];
            else res += m[60 - t % 60];
            m[t % 60]++;
        }
        return res;
    }
};
题解 2 - rust
- 编辑时间:2023-05-07
- 执行用时:12ms
- 内存消耗:2.4MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
    pub fn num_pairs_divisible_by60(time: Vec<i32>) -> i32 {
        let mut m = std::collections::HashMap::<i32, i32>::new();
        let mut res = 0;
        for t in time {
            if t % 60 == 0 {
                res += m.get(&0).unwrap_or(&0);
            } else {
                res += m.get(&(60 - t % 60)).unwrap_or(&0);
            }
            *m.entry(t % 60).or_insert(0) += 1;
        }
        res
    }
}
题解 3 - python
- 编辑时间:2023-05-07
- 执行用时:88ms
- 内存消耗:19.6MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
    def numPairsDivisibleBy60(self, time: List[int]) -> int:
        m = Counter()
        res = 0
        for t in time:
            if t % 60 == 0:
                res += m[0]
            else:
                res += m[60-t % 60]
            m[t % 60] += 1
        return res