题目如下:

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. mysql中 key 、primary key 、unique key 和 index 有什么不同

    mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...

  2. composer国内镜像

    composer国内镜像 一.总结 一句话总结: 直接百度 “composer 国内镜像” 即可 直接运行:composer config -g repo.packagist composer htt ...

  3. jQuery FileUpload doesn't trigger 'done'

     https://stackoverflow.com/questions/14674999/jquery-fileupload-doesnt-trigger-done   If your server ...

  4. Android7.0后JNI库必须保留Section Headers

    此修改在官网的描述如下: Each ELF file has additional information contained in the section headers. These header ...

  5. 20150722---点击按钮使指定的控件可见部分平移(JS)

    前段代码: <div id="out" style=" width:400px;overflow:hidden;"> <div id=&quo ...

  6. -bash: ./hello.jar: 无法执行二进制文件

    在linux中直接调用java包产生的 解决:依赖多个包要用冒号分隔,而不是分号 正确:> java -cp ./lib/*:./hello.jar hello 错误:> java -cp ...

  7. 大数据学习笔记之Zookeeper(四):Zookeeper实战篇(二)

    文章目录 4.1 分布式安装部署 4.2 客户端命令行操作 4.3 API应用 4.3.1 eclipse环境搭建 4.3.2 创建ZooKeeper客户端: 4.3.3 创建子节点 4.3.4 获取 ...

  8. Grafana 下载与安装(v5.4.1)

    官网地址: https://grafana.com/grafana/download Linux Ubuntu & Debian(64 Bit) SHA256: 3ccbdba9e7429f5 ...

  9. spring-第十二篇之两种后处理器

    1.扩展IoC容器使用后处理器扩展 bean后处理器:对容器中的bean进行后处理,也就是额外的加强. 容器后处理:对IoC容器进行后处理,增强容器功能. 2.bean后处理器      负责处理容器 ...

  10. [CF643E]Bear and Destroying Subtrees(期望,忽略误差)

    Description: ​ 给你一棵初始只有根为1的树 ​ 两种操作 ​ 1 x 表示加入一个新点以 x为父亲 ​ 2 x 表示以 x 为根的子树期望最深深度 ​ 每条边都有 \(\frac{1}{ ...