码迷,mamicode.com
首页 > 其他好文 > 详细

463. Island Perimeter

时间:2018-09-28 10:58:26      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:lan   sla   bsp   length   ==   isl   pre   nbsp   class   

 1 class Solution {
 2     public int islandPerimeter(int[][] grid) {
 3         if(grid == null) return 0;
 4         int row = grid.length;
 5         if(row == 0) return 0;
 6         int col = grid[0].length;
 7         int res = 0;
 8         for(int i = 0; i < row; i++) {
 9             for(int j = 0; j < col; j++) {
10                 if(grid[i][j] == 1) {
11                     res += 4;
12                     if(i+1 < row && grid[i+1][j] == 1) {
13                         res -= 1;
14                     }
15                     if(i-1 >= 0 && grid[i-1][j] == 1) {
16                         res -= 1;
17                     }
18                     if(j+1 < col && grid[i][j+1] == 1) {
19                         res -= 1;
20                     }
21                     if(j-1 >= 0 && grid[i][j-1] == 1) {
22                         res -= 1;
23                     }
24                 }
25             }
26         }
27         return res;
28         
29     }
30 }

 

463. Island Perimeter

标签:lan   sla   bsp   length   ==   isl   pre   nbsp   class   

原文地址:https://www.cnblogs.com/goPanama/p/9716571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!