【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 < 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 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 <= 60000
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的更多相关文章
- 【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 ...
随机推荐
- sqlalchemy.exc.IntegrityError: (mysql.connector.errors.IntegrityError) 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
排错: 看到 Duplicate entry '1' for key 'PRIMARY'是主键错误 看一看自己添加的数据id是1 再查询一下数据库中的表,发现id=1的记录已经存在了 所以在代码中让i ...
- Git学习及使用
一.认知git理论 1.git出现的背景 版本控制 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会改名加上 ...
- win2019
slmgr /upkslmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464Cslmgr /skms zh.us.toslmgr /ato
- 《图解设计模式》读书笔记3-2 Prototype模式
目录 Prototype(原型)模式的由来 类图 代码 角色 我的理解 Prototype(原型)模式的由来 创建一个实例,可以关键字new创建.但有时候,我们需要在不指定类名的前提下生成实例,比如: ...
- java反射机制-简单实例
public class Car { private String brand; private String color; private int maxSpeed; public Car() { ...
- symfony 初始化项目
学习Symfony首先看一下已经发布了哪些版本; 现在我记录一下两个版本的使用情况: 3.4 是一个长期维护且稳定的版本 4.3是一个最新版本且速度飞快地版本 官方介绍:https://symfony ...
- 【ABAP系列】SAP ABAP-模块 字符串操作基础知识
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP-模块 字符串操 ...
- 怎么查看keras 或者 tensorflow 正在使用的GPU
查看keras认得到的GPU from keras import backend as K K.tensorflow_backend._get_available_gpus() Out[28]: [' ...
- Robotframework使用自写库连接mysql数据库
Robotframework使用自写库连接mysql数据库 新建库文件mysqltest.py 代码如下: # -*- coding: utf-8 -*- import MySQLdbimport o ...
- python操作mysql之增删改查
[insert] import MySQLdb conn = MySQLdb.connect(","08day5" ) cur = conn.cursor() #把数据放 ...