当前位置:网站首页>Leetcode-6125: equal row and column pairs
Leetcode-6125: equal row and column pairs
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-6125: Equal row and column pairs
subject
I'll give you a subscript from 0 Start 、 The size is n x n The integer matrix of grid , Return to satisfaction Ri Row sum Cj Row and column pairs with equal columns (Ri, Cj) Number of .
If rows and columns contain the same elements in the same order ( Equal array ), They are equal .
Example 1:
Input :grid = [[3,2,1],[1,7,6],[2,7,7]]
Output :1
explain : There is a pair of equal row and column pairs :
- ( The first 2 That's ok , The first 1 Column ):[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
explain : There are three pairs of equal row and column pairs :
- ( The first 0 That's ok , The first 0 Column ):[3,1,2,2]
- ( The first 2 That's ok , The first 2 Column ):[2,4,2,2]
- ( The first 3 That's ok , The first 2 Column ):[2,4,2,2]

Problem solving
Method 1 : Code as string+ Hash
Set the horizontal direction , Code as string
Set the vertical direction , Programming string,
For example, for :
{
{1,11},
{11,1}
}
horizontal direction , Can be coded as "#1#11" and "#11#1"
In the vertical direction , Can be coded as "#1#11" and "#11#1"
So just record it in map Record each string Number of occurrences , Then let the horizontal string And vertical string Is the multiplication of numbers .
( Code as string It's more convenient , Compilation provides a pair of string Hash function of , If it is vector As map Of key, Also customize the hash , More trouble )
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
int m=grid.size(),n=grid[0].size();
unordered_map<string,int> map_row;
unordered_map<string,int> map_col;
string row_str,col_str;
for(int i=0;i<m;i++){
row_str.clear();
for(int j=0;j<n;j++){
row_str+="#"+to_string(grid[i][j]);
}
map_row[row_str]++;
}
for(int i=0;i<n;i++){
col_str.clear();
for(int j=0;j<m;j++){
col_str+="#"+to_string(grid[j][i]);
}
map_col[col_str]++;
}
int res=0;
for(auto& it:map_row){
if(map_col.count(it.first)){
res+=it.second*map_col[it.first];
}
}
return res;
}
};
边栏推荐
- 火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
- [advanced mathematics] [1] function, limit, continuity
- Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
- preprocessor directives
- [workplace rules] it workplace rules | poor performance
- 【高等数学】【4】不定积分
- Docker builds redis cluster
- Mobile web layout method
- Kubernetes advanced part learning notes
- [advanced mathematics] [6] differential calculus of multivariate functions
猜你喜欢

【高等数学】【6】多元函数微分学

Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important

增加 swap 空间

Detailed explanation of document operation

Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

数据库清空表数据并让主键从1开始

程序的编译和运行

文件操作详解

leetcode-114:二叉树展开为链表
随机推荐
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
leetcode-79:单词搜索
String of sword finger offer question bank summary (II) (C language version)
leetcode-919:完全二叉树插入器
【高等数学】【3】微分中值定理与导数的应用
Apache Mina framework "suggestions collection"
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
Proxy implements MySQL read / write separation
【高等数学】【4】不定积分
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
【高等数学】【6】多元函数微分学
Kubernetes advanced part learning notes
雷达水位计的工作原理及安装维护注意事项
How much memory does bitmap occupy in the development of IM instant messaging?
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
数据库清空表数据并让主键从1开始
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
Remote—实战
Detailed explanation of document operation