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;
    }
};
相关推荐
地平线开发者8 小时前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮9 小时前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者9 小时前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考9 小时前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx13 小时前
CART决策树基本原理
算法·机器学习
Wect13 小时前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript
颜酱14 小时前
单调队列:滑动窗口极值问题的最优解(通用模板版)
javascript·后端·算法
肆忆_16 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星20 小时前
虚函数表:C++ 多态背后的那个男人
c++
Gorway20 小时前
解析残差网络 (ResNet)
算法