【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
题目如下:
In a list of songs, the
i-th song has a duration oftime[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 indicesi < jwith(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 60Example 2:
Input: [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.Note:
1 <= time.length <= 600001 <= 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的更多相关文章
- 【LeetCode】1013. Pairs of Songs With Total Durations Divisible by 60 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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/ 参考 ...
- [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 ...
- 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 ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【LEETCODE】51、数组分类,简单级别,题目:581,830,1010,665
package y2019.Algorithm.array; /** * @ClassName FindUnsortedSubarray * @Description TODO 581. Shorte ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
随机推荐
- [CSP-S模拟测试]:数论(数学)
题目传送门(内部题11) 输入格式 第一行,三个整数$T,K,M$,分别代表数据组数.良好标准和整数范围.接下来$T$行,每行一个整数$n_i$,代表一个询问. 输出格式 输出$T$行,在第$i$行对 ...
- How to pass values across the pages in ASP.net without using Session
https://stackoverflow.com/questions/14956027/how-to-pass-values-across-the-pages-in-asp-net-without- ...
- Why is HttpContext.Current null during the Session_End event?
Why is HttpContext.Current null during the Session_End event? On Session_End there is no communicati ...
- PADS软件
最近学习PADS,搜集到的一些软件.之前一直在使用Altium designer,但是AD太占资源了,还有都说PADS比AD好. 下面是来自网上对主流PCB的介绍(原文:http://9mcu.com ...
- React Hooks简单业务场景实战(非源码解读)
前言 React Hooks 是React 16.7.0-alpha 版本推出的新特性.从 16.8.0 开始,React更稳定的支持了这一新特性. 它可以让你在不编写 class 的情况下使用 st ...
- spring cloud服务间调用feign
参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...
- 2016/7/26-apache配置文件:http.conf配置详解
引用地址http://blog.chinaunix.net/uid-21807675-id-1814871.html Apache的配置文件http.conf参数含义详解 Apache的配置由http ...
- 企业SRC整理
0.SRCs|安全应急响应中心 - 0xsafe 1.腾讯安全应急响应中心(TSRC) 2.360安全应急响应中心 3.京东安全应急响应中心(JSRC) 4.平安集团安全应急响应中心(PSRC) 5. ...
- jmeter对响应数据做断言
单独校验某个接口中的某个字段时,断言就相当于检查点 添加http请求,输入路径url
- RMQ(连续相同最大值)
http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submi ...