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 == grid[i].length

1 <= n <= 200

1 <= grid[i][j] <= 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;
    }
};
相关推荐
zycoder.1 分钟前
力扣面试经典150题day3第五题(lc69),第六题(lc189)
算法·leetcode·面试
你的冰西瓜20 分钟前
C++动态规划入门指南——助力CSP竞赛夺冠
c++·动态规划
西阳未落2 小时前
LeetCode——双指针
c++·算法
胖咕噜的稞达鸭2 小时前
C++中的父继子承:继承方式实现栈及同名隐藏和函数重载的本质区别, 派生类的4个默认成员函数
java·c语言·开发语言·数据结构·c++·redis·算法
笑口常开xpr2 小时前
【C++】模板 - - - 泛型编程的魔法模具,一键生成各类代码
开发语言·数据结构·c++·算法
AA陈超3 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P05-01.创建游戏玩法标签
c++·游戏·ue5·游戏引擎·虚幻
立志成为大牛的小牛3 小时前
数据结构——十四、构造二叉树(王道408)
数据结构·笔记·学习·程序人生·考研
IT小番茄3 小时前
Kubernetes云平台管理实战:自动加载到负载均衡(七)
算法
笑口常开xpr3 小时前
【C++继承】深入浅出C++继承机制
开发语言·数据结构·c++·算法
代码AC不AC3 小时前
【C++】红黑树实现
c++·红黑树·底层结构