C++ straight-forward 9-line solution


  • 0
    M
        int countCornerRectangles(vector<vector<int>>& grid) {
            unordered_map<string, int> lines;
            for(int i=0;i<grid.size();i++) {
                for(int j=0;j<grid[0].size()-1;j++) {
                    if(grid[i][j]==0) continue;
                    for(int k=j+1;k<grid[0].size();k++) {
                        if(grid[i][k]==1) lines[to_string(j)+"#"+to_string(k)]++;
                    }
                }
            }
            int res=0;
            for(unordered_map<string, int>::iterator p=lines.begin();p!=lines.end();p++) res+=(p->second)*(p->second-1)/2;
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.