Loading

892. 三维形体的表面积

892. 三维形体的表面积
https://leetcode-cn.com/contest/weekly-contest-99/problems/surface-area-of-3d-shapes/
package com.test;

import java.util.Map;

/**
 * 892. 三维形体的表面积
 * https://leetcode-cn.com/contest/weekly-contest-99/problems/surface-area-of-3d-shapes/
 */
public class Lesson892 {
    public static void main(String[] args) {
        int[][] grid = {{1, 0}, {0, 2}};
        int i = surfaceArea(grid);
        System.out.println(i);
    }

    public static int surfaceArea(int[][] grid) {
        int sum = 0;
        for (int i = 0; i < grid.length; i++) {
            int[] ints = grid[i];
            for (int j = 0; j < ints.length; j++) {
                int anInt = ints[j];
                if (anInt < 1) {
                    continue;
                }
                sum = sum + anInt * 4 + 2;
                // 每个单元格向外考虑
                if (i + 1 < grid.length && grid[i + 1][j] > 0) {
                    int min = Math.min(anInt, grid[i + 1][j]);
                    sum = sum - 2 * min;
                }
                if (j + 1 < ints.length && grid[i][j + 1] > 0) {
                    int min = Math.min(anInt, grid[i][j + 1]);
                    sum = sum - 2 * min;
                }
            }
        }
        return sum;
    }
}

 

posted @ 2018-08-26 20:49  stono  阅读(304)  评论(0编辑  收藏  举报