首页 > 代码库 > LeetCode: Island Perimeter
LeetCode: Island Perimeter
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。