当前位置:网站首页>LeetCode 2352. Equal Row Column Pairs
LeetCode 2352. Equal Row Column Pairs
2022-07-30 01:28:00 【Michael Armin】
1. 题目
给你一个下标从 0 开始、大小为 n x n 的整数矩阵 grid ,返回满足 Ri 行和 Cj 列相等的行列对 (Ri, Cj) 的数目.
如果行和列以相同的顺序包含相同的元素(即相等的数组),则认为二者是相等的.
示例 1:

输入:grid = [[3,2,1],[1,7,6],[2,7,7]]
输出:1
解释:存在一对相等行列对:
- (第 2 行,第 1 列):[2,7,7]
示例 2:

输入:grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
输出:3
解释:存在三对相等行列对:
- (第 0 行,第 0 列):[3,1,2,2]
- (第 2 行, 第 2 列):[2,4,2,2]
- (第 3 行, 第 2 列):[2,4,2,2]
提示:
n == grid.length == grid[i].length
1 <= n <= 200
1 <= grid[i][j] <= 10^5
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/equal-row-and-column-pairs
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
2. 解题
- 暴力,Find the same number in the first column and the first row,然后再进行比较
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
n = len(grid)
m = defaultdict(list) # v: [idx ]
for i in range(n):
m[grid[0][i]].append(i)
def same(x, y):
for i in range(n):
if grid[x][i] != grid[i][y]:
return False
return True
ans = 0
for i in range(n):
for j in m[grid[i][0]]:
if same(i, j):
ans += 1
return ans
1976 ms 18.6 MB Python3
- 更优的做法,转置后得到的新矩阵 compare with the original
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
n = len(grid)
g2 = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
for j in range(n):
g2[i][j] = grid[j][i]
ans = 0
for i in range(n):
for j in range(n):
if g2[i] == grid[j]:
ans += 1
return ans
404 ms 18.5 MB Python3
我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
边栏推荐
猜你喜欢

my creative day

Leetcode69. x 的平方根
[email protected](using passwordYES)"/>Navicat error: 1045-Access denied for user [email protected](using passwordYES)

The solution to the bug, the test will no longer be blamed
![[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.](/img/29/a6ec7e00df289f68dcd39fe4f35fd3.png)
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.

Graphical LeetCode - 593. Valid Squares (Difficulty: Moderate)

Recommendation system: collection of user "behavioral data" [use Kafka and Cassandra to process data] [if it overlaps with business data, it also needs to be collected independently]

postgresql日常运维技能,适合初学者

Navicat for mysql crack version installation

Running a Fabric Application
随机推荐
Fabric Writing Case Chaincode
JS开发3D建模软件
[MySQL series] MySQL database foundation
CMake Tutorial 巡礼(0)_总述
液压滑环的应用介绍
Recommendation systems: feature engineering, common features
nacos集群配置详解
postgresql日常运维技能,适合初学者
Meetings OA To Be Meeting && All Meetings
Linux - install MySQL (detailed tutorial)
【MySQL总结】
my creative day
【微服务~Nacos】Nacos之配置中心
【MySQL必知必会】 范式 | ER模型
1.2Recyclerview实现Item点击事件
CMake Tutorial Tour (1)_Basic starting point
[Microservice~Nacos] Configuration Center of Nacos
记笔记!电源自动测试系统测试电源纹波详细方法
Towards Better Understanding of Self-Supervised Representations / Q-Score
自学HarmonyOS应用开发(53)- 获取当前位置