角矩形的数量

记录每个列和列之间的pair,然后下次用到就累加,降维的思想

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    int countCornerRectangles(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        vector<vector<int>> dp(n,vector<int>(n));
        int ans = 0;
        for(int row =0;row <m;row ++){
            for(int col =0;col < n;col++){
                if(grid[row][col]==1){
                    for(int col2 = col+1;col2<n;col2++){
                        if(grid[row][col2]==1){
                            ans += dp[col][col2];
                            dp[col][col2] ++;
                        }
                    }
                }
            }
        }
        return ans;
    }
};
算法小屋 文章被收录于专栏

不定期分享各类算法以及面经。同时也正在学习相关分布式技术。欢迎一起交流。

全部评论

相关推荐

只写bug的程序媛:才15,我招行20多万,建设银行50多万,说放弃就放弃
点赞 评论 收藏
分享
01-18 09:26
已编辑
门头沟学院 Java
王桑的大offer:建议中间件那块写熟悉即可,写掌握 面试包被拷打到昏厥
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务