其他分享
首页 > 其他分享> > 力扣第892题 三维形体的表面积

力扣第892题 三维形体的表面积

作者:互联网

力扣第892题 三维形体的表面积

class Solution {
    public:
    int surfaceArea(vector<vector<int>>& grid) 
    {
        int lenr = grid.size();
        if (lenr == 0)
            return 0;
        int lenc = grid[0].size();
        int dr[4] = { 0, 0, 1, -1 };
        int dc[4] = { -1, 1, 0, 0 };
        int result = 0;
        int r, c;
        for (int i = 0; i < lenr; i++)
        {
            for (int j = 0; j < lenc; j++)
            {
                if (grid[i][j] <= 0)
                    continue;
                result += 2; // 上下两个面
                for (int k = 0; k < 4; k++)
                {
                    r = i + dr[k];
                    c = j + dc[k];
                    

                    if (r < 0 || r >= lenr || c < 0 || c >= lenc)
                        result += grid[i][j];
                    else
                        result += max<int>(grid[i][j] - grid[r][c], 0); // 相邻的可能比当前的高,此时是0
                }
            }
        }
        return result;
    }

};

标签:lenc,892,int,表面积,力扣,lenr,grid,result,size
来源: https://www.cnblogs.com/woodjay/p/12571197.html