角矩形的数量

记录每个列和列之间的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;
    }
};
算法小屋 文章被收录于专栏

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

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务