首页 > 其他 > 详细

LeetCode: Island Perimeter

时间:2016-11-21 18:33:38      阅读:287      评论:0      收藏:0      [点我收藏+]
 1 public class Solution {
 2     public int islandPerimeter(int[][] grid) {
 3         int ans = 0;
 4         for (int i = 0; i < grid.length; i++) {
 5             for (int j = 0; j < grid[0].length; j++) {
 6                 if (grid[i][j] == 1) {
 7                     if (i == 0 || (i > 0 && grid[i-1][j] == 0)) ans++;  //top
 8                     if (i == grid.length-1 || (i < grid.length-1 && grid[i+1][j] == 0)) ans++; //bottom
 9                     if (j == 0 || (j > 0 && grid[i][j-1] == 0)) ans++; //left
10                     if (j == grid[0].length-1 || (j < grid[0].length-1 && grid[i][j+1] == 0)) ans++; //right
11                 }
12             }
13         }
14         return ans;
15     }
16 }

 

LeetCode: Island Perimeter

原文:http://www.cnblogs.com/yingzhongwen/p/6085965.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!