本文由本人@takooctopus对LeetCode每日打卡签到,做点记录。
892. Surface Area of 3D Shapes
题目描述
On a N * N grid, we place some 1 * 1 * 1 cubes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
Return the total surface area of the resulting shapes.
Example
| Example 1: | |
|---|---|
| Input: | [[2]] |
| Output: | 10 |
| Example 2: | |
|---|---|
| Input: | [[1,2],[3,4]] |
| Output: | 34 |
| Example 3: | |
|---|---|
| Input: | [[1,0],[0,2]] |
| Output: | 16 |
| Example 4: | |
|---|---|
| Input: | [[1,1,1],[1,0,1],[1,1,1]] |
| Output: | 32 |
| Example 5: | |
|---|---|
| Input: | [[2,2,2],[2,1,2],[2,2,2]] |
| Output: | 46 |
Note:
{{- code -}}
Solution
我做的时候比较麻烦,速度比较慢。
{{- code -}}
我做的速度比较慢
{{- code -}}
看其他人的,大约比我快了12ms,我看了其中最为主要的一点是他在加边时只选了上左两边,相比于我将4个方向全加了能省下一半的工作,看起来我的考虑不够周到。
{{- code -}}
这个一句话的程序…
只能说原来可以这样简化句子,这个一句话程序和我的思想差不多,主要是注意2 for row in grid for v in row if v这样的简化思想。