题目如下:

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. python中私有属性的访问

    class MyClass(): def __init__(self): self.__superprivate = "Hello" self.__semiprivate = &q ...

  2. jmeter 命令行运行与生成报告

    一.     使用命令行方式运行Jmeter 1.1 为什么 使用GUI方式启动jmeter,运行线程较多的测试时,会造成内存和CPU的大量消耗,导致客户机卡死. 所以正确的打开方式是在GUI模式下调 ...

  3. 从DBA_DDL_LOCKS视图获得DDL锁定信息

    http://liwenshui322.iteye.com/blog/1166934 DDL锁有三种: 1.排他DDL锁(Exclusive DDL lock):这会防止其他会话得到它们自己的DDL锁 ...

  4. Ntdll.h

    转自:https://www.cnblogs.com/gwsbhqt/p/5092390.html 为了能使用上Ntdll.lib库函数,从几份不完整的Ntdll.h中拼凑整理出了比较完整美观的Ntd ...

  5. Jenkins持续集成_01_Mac安装配置

    前言 Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建.测试和部署软件.在自动化测试中,用来持续集成,跑定时任务进行定时自动化监测.更详细介绍可查看jenkins官网: ...

  6. 【题解】4879. 【NOIP2016提高A组集训第11场11.9】少女觉

    Description 在幽暗的地灵殿中,居住着一位少女,名为古明地觉.据说,从来没有人敢踏入过那座地灵殿,因为人们恐惧于觉一族拥有的能力——读心.掌控人心者,可控天下. 咳咳.人的记忆可以被描述为一 ...

  7. 题解[SCOI2009]粉刷匠 难度:省选/NOI-

    Description windy有 N 条木板需要被粉刷.每条木板被分为 M 个格子.每个格子要被刷成红色或蓝色.windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色.每个格子最多 ...

  8. OJ测试数据追溯方法

    https://blog.csdn.net/iwts_24/article/details/79240987 我是从这个博客知道还有从比赛官方网站扒测试数据的方法,但是下面的链接有些是失效的. 所以我 ...

  9. 逻辑回归提高阈值对p和r的影响

    这里我做了一个实验 也就是随着阈值的增大,precision增加或者不变,recall减少或者不变.

  10. 05 - Jmeter连接多台电脑做压力测试

     在使用Jmeter进行接口的性能测试时, 由于Jmeter是JAVA应用, 对于CPU的内存消耗比较大, 所以, 当需要模拟数以万计的的并发用户时, 使用单台机器模拟所有用户并发就会有些力不从心了, ...