zoukankan      html  css  js  c++  java
  • 【leetcode】总持续时间可被 60 整除的歌曲

    在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。

    返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望索引的数字 i 和 j 满足  i < j 且有 (time[i] + time[j]) % 60 == 0。

    示例 1:

    输入:[30,20,150,100,40]
    输出:3
    解释:这三对的总持续时间可被 60 整数:
    (time[0] = 30, time[2] = 150): 总持续时间 180
    (time[1] = 20, time[3] = 100): 总持续时间 120
    (time[1] = 20, time[4] = 40): 总持续时间 60
    示例 2:

    输入:[60,60,60]
    输出:3
    解释:所有三对的总持续时间都是 120,可以被 60 整数。
     

    int numPairsDivisibleBy60(int* time, int timeSize){
        int i,j,count=0,hash[60]={0};
        for (i=0; i<timeSize; i++) hash[time[i]%60]++;
        count += hash[0] * (hash[0]-1)/2;
        count += hash[30] * (hash[30]-1)/2;
        i=1,j=59;
        while(i<j)
        {
            count += hash[i++] * hash[j--];
        }
        return count;
    }
  • 相关阅读:
    学习自建调试体系(二)
    寻找未导出函数的函数地址
    Http
    git忽略.gitignore
    Flask-sqlacodegen
    liunx速查
    BBS论坛项目
    vim操作
    部署
    python3 环境搭建
  • 原文地址:https://www.cnblogs.com/ganxiang/p/13662256.html
Copyright © 2011-2022 走看看