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;
    }
};
相关推荐
Tairitsu_H20 分钟前
[LC优选算法#18] 前缀和 | 除⾃⾝以外数组的乘积 | 和为K的⼦数组 | 和可被K整除的⼦数组
算法·前缀和·哈希算法
鹏易灵37 分钟前
C++——8.移动语义初探(移动构造、移动赋值)
开发语言·c++·php
凯瑟琳.奥古斯特1 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展
Alvin's Tech Blog1 小时前
内存泄漏会导致操作系统永久丢失内存吗?它会导致应用程序崩溃吗?
c++·内存管理·内存泄漏
会周易的程序员1 小时前
使用 LuaBridge 封装 C++ 日志库 microLog 为 Lua 模块
c++·物联网·junit·lua·日志·iot·aiot
wbs_scy2 小时前
仿 muduo 高并发服务器项目:从 timerfd 到时间轮实现定时任务机制
linux·服务器·c++
aqiu1111112 小时前
【算法日记 13】LeetCode 49. 字母异位词分组:哈希表的进阶“降维打击”
算法·leetcode·散列表
WL学习笔记2 小时前
链式队列(数据结构)
前端·数据结构·jquery
KaMeidebaby2 小时前
卡梅德生物技术快报|纳米抗体技术全套实操流程:AFB1 全合成文库淘选 + 分子对接定点突变参数手册
人工智能·python·tcp/ip·算法·机器学习
巴糖2 小时前
从做早餐理解 DAG 与拓扑排序:任务依赖图的核心逻辑
算法