892. Surface Area of 3D Shapes

 emmm,好像到了下午,思路就不清楚了,脑袋不清楚,直接看了discussion

既然自己没有想出来,那就记住他!嗯!这个是每次检查左边和后面,

 class Solution {
public:
    int surfaceArea(vector<vector<int>>& grid) {
        int l = grid.size();
        int res = 0;
        for( int i = 0 ; i < l ; i ++ ){
            for( int j = 0 ; j < l ; j ++ ){
                if( grid[ i ][ j ] != 0 )res += grid[ i ][ j ] * 4 + 2;
                if( i >= 1 )res -= min( grid[ i ][ j ] , grid[ i - 1 ][ j ] ) * 2;
                if( j >= 1 )res -= min( grid[ i ][ j ] , grid[ i ][ j - 1 ] ) * 2;
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_40801853/article/details/82387387