class Solution {
public:
long long numberOfRightTriangles(vector<vector<int>>& grid) {
long long res = 0;
vector<int> row(grid.size());
vector<int> col(grid[0].size());
//计算每一行有多少个1
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[i].size(); j++){
row[i] += grid[i][j];
}
}
//计算每一列有多少个1
for(int j = 0; j < grid[0].size(); j++){
for(int i = 0; i < grid.size(); i++){
col[j] += grid[i][j];
}
}
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[i].size(); j++){
if(grid[i][j] == 1){
res += (col[j] - 1) * (row[i] - 1);
}
}
}
return res;
}
};
LeetCode3128.直角三角形
最新推荐文章于 2024-11-10 22:42:55 发布