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;
    }
};
相关推荐
凌波粒3 分钟前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
旖-旎9 分钟前
《LeetCode 417 太平洋大西洋水流问题 FloodFill DFS 解法》
c++·算法·深度优先·力扣·floodfill
凌波粒12 分钟前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode
鱼子星_17 分钟前
C++从零开始系列篇(二):C++入门——函数重载,引用,inline与nullptr
开发语言·c++·笔记
2zcode26 分钟前
项目文档:基于MATLAB语音信号变声算法设计与实现
算法·matlab·语音识别
指令集梦境31 分钟前
图解:单调栈算法模板(Java语言)
java·开发语言·算法
小灰灰搞电子40 分钟前
C++ boost::circular_buffer 详解:原理、用法与实战
开发语言·c++·boost
生成论实验室1 小时前
自动驾驶:一个自主运动的系统
人工智能·算法·机器学习·语言模型·机器人·自动驾驶·安全架构
sheeta19981 小时前
LeetCode 每日一题笔记 日期:2026.06.16 题目:3612. 字符串特殊符号处理
笔记·算法·leetcode
CoderYanger1 小时前
A.每日一题:2095. 删除链表的中间节点
java·数据结构·程序人生·leetcode·链表·面试·职场和发展