【leetcode】1128. Number of Equivalent Domino Pairs
题目如下:
Given a list of
dominoes,dominoes[i] = [a, b]is equivalent todominoes[j] = [c, d]if and only if either (a==candb==d), or (a==dandb==c) - that is, one domino can be rotated to be equal to another domino.Return the number of pairs
(i, j)for which0 <= i < j < dominoes.length, anddominoes[i]is equivalent todominoes[j].Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1Constraints:
1 <= dominoes.length <= 400001 <= dominoes[i][j] <= 9
解题思路:题目本身不难,遍历dominoes,依次把(dominoes[i][0],dominoes[i][1])当做key值存入字典中,存入之前,先求出(dominoes[i][0],dominoes[i][1])出现的次数,出现了多少次就说明在0~i-1区间内与其等价的个数。如果dominoes[i][0]不等于dominoes[i][1],那么还需加上(dominoes[i][1],dominoes[i][0])在字典中出现的次数。
代码如下:
class Solution(object):
def numEquivDominoPairs(self, dominoes):
"""
:type dominoes: List[List[int]]
:rtype: int
"""
dic = {}
res = 0
for (i,j) in dominoes:
if (i,j) in dic:
res += dic[(i,j)]
if i !=j and (j,i) in dic:
res += dic[(j,i)]
dic[(i,j)] = dic.setdefault((i,j),0) + 1
return res
【leetcode】1128. Number of Equivalent Domino Pairs的更多相关文章
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
- 【Leetcode_easy】1128. Number of Equivalent Domino Pairs
problem 1128. Number of Equivalent Domino Pairs solution1: 不明白为什么每个元素都要加上count: class Solution { pub ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- LeetCode.1128-等价多米诺骨牌对的数量(Number of Equivalent Domino Pairs)
这是小川的第394次更新,第428篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第259题(顺位题号是1128).给定多米诺骨牌列表,当且仅当(a == c且b == d ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
随机推荐
- lambda一些查询语句
<!--得分数据结构-->1 <Score> <studentid>1</studentid> <courseid>1</course ...
- [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall
SUMMARY: This article provides information on how to manually generate a new system self-signed cert ...
- Java第五周作业+总结
实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 1.已知字符串:"this is a test of java".按要求执 ...
- 解决MySql忘记密码
描述:忘记了mysql的登录密码,无法登录的情况下该怎么办? 环境:CentOS 7,数据库:mysql 5.7 1.停止数据库(先查看mysql服务是否运行) # ps -ef | -i grep ...
- Java数据结构之递归(Recursion)
1. 递归解决问题 各种数学问题如:8皇后问题,汉诺塔,阶乘问题,迷宫问题,球和篮子的问题(google编程大赛) 各种算法中也会使用到递归,比如快速排序,归并排序,二分查找,分治算法等 将用栈解决的 ...
- Charles的app抓包配置和抓包节点为unknown、乱码、手机无法上网等问题的处理
关于手机端抓包配置Charles的一些配置,并且解决,请求存在unknown和内容为乱码的问题 关于请求节点为unknown.内容为乱码和手机端无法访问网络其实都是手机端证书配置问题导致,有的配置后还 ...
- 如何通过PHP将excel的数据导入MySQL中
在开发PHP程序时,很多时候我们会有将数据批量导入数据库的需求,如学生信息批量添加到数据库中,而事先用excel编排好,对excel实行操作,便是我们比较常用的选择方式. 在对excel的操作中,ph ...
- S-阶乘除法
输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1). 比如,若 n=6, m=3,则n!/m!=6!/3!=720/6=120. 是不是很简 ...
- 使用Docker部署Spring-Boot+Vue博客系统
在今年年初的时候,完成了自己的个Fame博客系统的实现,当时也做了一篇博文Spring-boot+Vue = Fame 写blog的一次小结作为记录和介绍.从完成实现到现在,也断断续续的根据实际的使用 ...
- python学习第五十四天hashlib模块的使用
hash算法 hash也做散列,也称为哈希,主要用于信息安全领域中加密算法,hash就是找一种数据内容和数据存放地址直接的映射关系. md5算法 md5讯息算法,广泛使用密码函数 md5算法的特点 1 ...