leetcode463: Island Perimeter
来源:互联网 发布:微信域名检测 php源码 编辑:程序博客网 时间:2024/06/01 09:58
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example:
[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]Answer: 16Explanation: The perimeter is the 16 yellow stripes in the image below:算一算像图中这样摆放的“小岛”有多少条边界(黄色的线段),遍历一遍整个数组,有一个“1”就加4,有一个相邻就减1
public int islandPerimeter(int[][] grid) {int count = 0;int result = 0;for (int i = 0; i < grid.length; i++)for (int j = 0; j < grid[0].length; j++) {if (grid[i][j] == 1)result++;if (grid[i][j] == 1) {if (i != 0)if (grid[i - 1][j] == 1)count++;if (j != 0)if (grid[i][j - 1] == 1)count++;if (i != grid.length - 1)if (grid[i + 1][j] == 1)count++;if (j != grid[0].length - 1)if (grid[i][j + 1] == 1)count++;}}result = result * 4 - count;return result;}
0 0
- [LeetCode463]Island Perimeter
- leetcode463: Island Perimeter
- leetcode463 Island Perimeter JAVA
- leetcode463. Island Perimeter
- [LeetCode463]Island Perimeter
- Leetcode463. Island Perimeter
- LeetCode463 island perimeter
- leetcode463. Island Perimeter
- Leetcode463. Island Perimeter
- python--leetcode463. Island Perimeter
- 22.leetCode463: Island Perimeter
- [LeetCode463]Island Perimeter岛屿周长问题
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Island Perimeter
- Mobile Security Framework分析apk
- 1198
- 去重,重复的去掉
- SharedPreferences存储
- java并发编程实践学习(13 ) 显示锁
- leetcode463: Island Perimeter
- 文件
- VS2010 + OpenGL 及 第一个实例
- C#中数组、ArrayList和List三者的区别
- Linux学习之——常用目录的作用
- winform中contextMenuStrip使用
- 性能优化之快速响应的用户界面
- POJ 1769 Minimizing maximizer(最少区间覆盖dp + 线段树优化查询区间最小值)
- 【Tensorflow】Windows下基于Anaconda的Tensorflow环境配置