LeetCode Weekly Contest 146
1128. Number of Equivalent Domino Pairs
Given a list of dominoes
, dominoes[i] = [a, b]
is equivalent to dominoes[j] = [c, d]
if and only if either (a==c
and b==d
), or (a==d
and b==c
) - that is, one domino can be rotated to be equal to another domino.
Return the number of pairs (i, j)
for which 0 <= i < j < dominoes.length
, and dominoes[i]
is equivalent to dominoes[j]
.
Example 1:
Input: dominoes = [[1,2],[2,1],[3,4],[5,6]] Output: 1
Constraints:
1 <= dominoes.length <= 40000
1 <= dominoes[i][j] <= 9
题目大意:给你一个数字键值对数组,让你判断有多少个数组元素是相等的。两个键值对相等的条件是:键1=键2并且值1=值2或者键1=值2并且键2=值1。
思路:将键值的和中放前面加上“_”以及键值中较小的数作为map的key存入map,然后遍历map求和就好。(其实可以将键值中较小的数乘100或者1000然后加上大的那个数作为key,但是竞赛的时候,脑子没转过来,就用了这个方法,也是AC了)

class Solution { public int numEquivDominoPairs(int[][] dominoes) { Map<String, Integer> map = new HashMap<>(); int len = dominoes.length; for(int i=0; i<len; i++) { int a = dominoes[i][0]; int b = dominoes[i][1]; String te = a+b + "-"; if( a > b ) te = te + b; else te = te + a; Integer cnt = map.get(te); if( cnt==null ) { map.put(te, 1); } else { map.put(te, cnt+1); } } int sum = 0; for(Map.Entry<String, Integer> entry : map.entrySet() ) { int v = entry.getValue(); sum += (v*(v-1))/2; } return sum; } }
低调做人,高调做事。
标签:
LeetCode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2016-07-28 ACM题目————滑雪
2016-07-28 ACM题目————次小生成树