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;
    }
};
相关推荐
(╹◡╹)43 分钟前
18.剪枝
算法·机器学习·剪枝
布莱克6051 小时前
数据库索引分类:数据结构、物理存储与逻辑角度详解
数据结构·数据库
Fa_Mian_Tuan2 小时前
图论基础|邻接矩阵超详细讲解(含无向/有向/带权图+完整可运行C语言代码)
c语言·数据结构·笔记·算法·图论
hanhahai2 小时前
指针与函数(函数指针与指针函数)
算法
码匠许师傅2 小时前
【C++ 面试真题】聊聊 C++ 的序列容器
java·c++·面试
ValhallaCoder2 小时前
Leetcode-hot100(2026.08.17)
python·算法·leetcode
C++ 老炮儿的技术栈2 小时前
Qt5.9.1 Windows 完整开发环境搭建流程
开发语言·c++·windows·qt·编辑器·代码化
泡沫冰@3 小时前
GO 语言基础
开发语言·算法·golang
键盘会跳舞3 小时前
C++:std::tuple 源码级深度拆解——变参模板、SFINAE与模板元编程核心技巧
开发语言·c++·sfinae·变参模板
bkspiderx3 小时前
从零开始:VS Code搭建C/C++开发环境全指南(Windows/macOS/Linux)
c语言·c++·windows·vs code·c/c++开发环境