题目如下:

In a list of songs, the i-th song has a duration of time[i] seconds.

Return the number of pairs of songs for which their total duration in seconds is divisible by 60.  Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.

Example 1:

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

Example 2:

Input: [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.

Note:

  1. 1 <= time.length <= 60000
  2. 1 <= time[i] <= 500

解题思路:遍历Input并对其中每个元素与60取模,以余数为key值存入字典dic中,字典的value也key值作为余数出现的次数。接下来再遍历一次Input,求出元素与60取模后的余数,再求出60减去余数的差值,字典dic[差值]所对应的值即为这个元素可以与数组中多少个元素的和能被60整除。

代码如下:

class Solution(object):
def numPairsDivisibleBy60(self, time):
"""
:type time: List[int]
:rtype: int
"""
dic = {}
for i in time:
v = i % 60
dic[v] = dic.setdefault(v,0) + 1
res = 0
for i in time:
v = i % 60
dic[v] -= 1
key = 60 -v if v != 0 else 0
if key in dic:
res += dic[key]
return res

【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60的更多相关文章

  1. 【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 128th LeetCode Weekly Contest Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  3. 1013. Pairs of Songs With Total Durations Divisible by 60总持续时间可被 60 整除的歌曲

    网址:https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/submissions/ 参考 ...

  4. [Swift]LeetCode1010. 总持续时间可被 60 整除的歌曲 | Pairs of Songs With Total Durations Divisible by 60

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  5. Pairs of Songs With Total Durations Divisible by 60 LT1010

    In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of s ...

  6. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  7. 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665

    package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...

  8. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  9. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

随机推荐

  1. Spring Boot 的单元测试

    Spring Boot 的单元测试 引入依赖 testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-tes ...

  2. centos环境下安装java环境

    1-上java官网下载rpm安装包 jdk-7u80-linux-i586.rpm 2-新建文件夹,并把jdk-7u80-linux-i586.rpm上传到此文件夹下面 cd /usr/local/ ...

  3. win10下VMware15运行ubuntu18.04无法和主机之间复制粘贴问题

    可以运行以下命令行: sudo apt-get autoremove open-vm-tools sudo apt-get install open-vm-tools sudo apt-get ins ...

  4. php中数组的指针

    利用PHP内置的函数 key() 获得键. current()获得值, next(); prev();移动到上一个 reset();//重置,移动到第一个元素 end();//移动到最后一个元素上 注 ...

  5. MySQL 安装示例数据库(employee、world、sakila、menagerie 等)

    sakila 示例数据库官方资料及安装说明,注意查看示例数据库支持的版本是否匹配你的数据库. 为了测试,有时候需要大量的数据集,MySQL 官方提供了用于测试的示例数据库,下载页面在 这里. 下面以 ...

  6. 去JQUERY化

    时间 2016-05-17 12:43:59  OurJS 原文  http://ourjs.com/detail/573a9cec88feaf2d031d24fc 主题 jQuery 这是一篇使用原 ...

  7. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  8. python eval( ) 使用详解

      1.解析表达式 (表达式是str类型)----最常用     a = 12     b = "联播"     result1 = eval(a+3)        # resu ...

  9. mysql (mariadb)表结构添加修改删除方法总结

    1,添加表字段 alter table table1 add ptel varchar(100) not Null; alter table table1 add id int unsigned no ...

  10. python 实现加法

    https://ac.nowcoder.com/acm/contest/338/G 链接:https://ac.nowcoder.com/acm/contest/338/G来源:牛客网 题目描述 Th ...