LeetCode75——Day23

文章目录

一、题目

2352. Equal Row and Column Pairs

Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.

A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).

Example 1:

Input: grid = \[3,2,1,1,7,6,2,7,7]

Output: 1

Explanation: There is 1 equal row and column pair:

  • (Row 2, Column 1): 2,7,7
    Example 2:

Input: grid = \[3,1,2,2,1,4,4,5,2,4,2,2,2,4,2,2]

Output: 3

Explanation: There are 3 equal row and column pairs:

  • (Row 0, Column 0): 3,1,2,2
  • (Row 2, Column 2): 2,4,2,2
  • (Row 3, Column 2): 2,4,2,2

Constraints:

n == grid.length == gridi.length

1 <= n <= 200

1 <= gridij <= 105

二、题解

使用map保存键值对(vector<int>作为key,该数组的数目作为值),map底层禹unordered_map不同,基于红黑树

cpp 复制代码
class Solution {
public:
    int equalPairs(vector<vector<int>>& grid) {
        int n = grid.size();
        map<vector<int>,int> map;
        //添加行
        for(int i = 0;i < n;i++){
            map[grid[i]]++;
        }
        int res = 0;
        for(int j = 0;j < n;j++){
            //添加列
            vector<int> arr;
            for(int i = 0;i < n;i++){
                arr.push_back(grid[i][j]);
            }
            //如果存在对应的行与其相等,加上对应的行的数目
            if(map.find(arr) != map.end()) res += map[arr];
        }
        return res;
    }
};
相关推荐
武子康1 小时前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
David猪大卫2 小时前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger2 小时前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
vvilkim3 小时前
数据结构类型、区别与应用场景:一篇讲透选型逻辑
数据结构
文丑颜不良啊4 小时前
LeetCode2461~2480题目解析
java·leetcode
努力努力再努力wz5 小时前
【Lua 进阶系列】从 C++ 对象到 Lua 变量:一文理解 userdata、Lua 栈与元表绑定机制
c++·junit·lua
慧都产品智能化5 小时前
Qt 表格升级攻略:复用已有编辑器,减少重复开发
c++·qt·编辑器·qt6.3
ShineWinsu5 小时前
对于Qt:QTableWidget常见属性以及QTableWidgetItem类的解析
开发语言·c++·qt·gui·客户端·表格·qtablewidget
文丑颜不良啊5 小时前
LeetCode2441~2460题目解析
java·leetcode
ShineWinsu7 小时前
对于Qt:QListWidget常见属性以及QListWidgetItem类的解析
开发语言·c++·qt·客户端·列表·qlistwidget·qlistwidgetitem