原题链接
每个方向进行遍历,找到最大值就好了 代码如下:
class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int ans = 0;
int x = 0, y = 0, o = 0;
int n = grid.size();
if(!n) return 0;
int m = grid[0].size();
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
if(grid[i][j] != 0)
o ++;
for(int i = 0; i < n; i ++){
int k = INT_MIN;
for(int j = 0; j < m; j ++)
k = max(k, grid[i][j]);
x += k;
}
for(int i = 0; i < m; i ++){
int k = INT_MIN;
for(int j = 0; j < n; j ++)
k = max(k, grid[j][i]);
y += k;
}
ans = o + x + y;
return ans;
}
};
|