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

LeetCode: Island Perimeter

时间:2016-11-21 18:36:02      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:++   length   lan   int   log   sla   amp   i+1   style   

 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

标签:++   length   lan   int   log   sla   amp   i+1   style   

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

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