【leetcode】 463. Island Perimeter
题目: 以二维数组形式表示坐标岛屿,求边长。
例子:
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]] Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:

思路: 一开始想用最笨的办法,就是两次for循环遍历所有元素,如果为1(1为岛屿),就分别判断 上、下、左、右 是否为岛屿,若不是则 边数+1 。
第二次换了想法, 每一条横向 如果有岛屿,只要连续,那么左右两边和始终为2,如果不连续,则左右两边和 +2。 纵向判断 上下边 也是如此。
所用tag记录上一格是否为岛屿 来判断是否连续,如果为连续,则这一横排的 左右边和 始终为2, 如果有一个不连续,则左右边和 +2 。
横向判断用两次for循环,纵向判断也用两次for循环。第二层循环之前要把 tag清空,否则上一行的最后一格 和 下一行的第一格 会误判。
效率一般,更好的没想起来- -
public class Solution {
public int islandPerimeter(int[][] grid) {
int m = 0,tag = 0; //m记录边数 ,tag作为标记 记录是否为连续岛屿
int rows = grid.length;
int columns = grid[0].length;
for(int i = 0;i < rows;i++){ //横向遍历
tag = 0 ; //下层循环 标记清零
for(int j = 0; j < columns; j++){
if(grid[i][j] == 1){
if(tag == 1){
continue;
}
m = m + 2;
tag = 1;
}
else{
tag = 0;
}
}
}
for(int j = 0; j < columns; j++){ //纵向遍历
tag = 0; //下层循环 标记清零
for(int i = 0;i < rows;i++){
if(grid[i][j] == 1){
if(tag == 1){
continue;
}
m = m + 2;
tag = 1;
}
else{
tag = 0;
}
}
}
return m;
}
}
【leetcode】 463. Island Perimeter的更多相关文章
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【leetcode】976. Largest Perimeter Triangle
题目如下: Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- js对象深拷贝、浅拷贝
浅拷贝1 //浅拷贝1 let obj01 = { name: 'Lily', age: '20', time: ['13', '15'], person: { name: 'Henry', age: ...
- Arch linux(UEFI+GPT)安装及后续优化教程
Arch Linux安装过程中需要从远程存储库获取软件包,电脑需要有效的互联网连接. 1.联网 查看是否有网 ping www.baidu.com 同步时间 timedatectl set-ntp t ...
- leetcode-easy-others-190. Reverse Bits-NO
mycode 不会... 参考: 1. 思路: 将十进制的n转换成二进制(str) -> 利用切片.反向获取不包含0b的反转后的二进制字符串 -> 补上0(共32位) 2. class S ...
- Uep弹窗showModalDialog的使用
function imageMaintain() { $.showModalDialog($$pageContextPath + "uepI/imageMaintain.do?service ...
- C# CancellationTokenSource.Cancel 取消线程很鸡肋?
例子: CancellationTokenSource cts ; void MainWindow_Loaded(object sender, RoutedEventArgs e) { Task.Ru ...
- 为解决Thymeleaf数字格式化问题而想到的几种方案
背景: spring后端输出double类型数据,前端使用thymeleaf框架,格式化double数据类型,由于需要显示原值(比如原来录入5,而不能显示5.00),因此需要存储数值(存储值为deci ...
- Elasticsearch 6.2.3版本 同一个index新增type报错 Rejecting mapping update to [website] as the final mapping would have more than 1 type: [blog2, blog]
在website的index下已经存在一个名为blog的type.想在website下,新增一个名为blog2的type. 执行语句如下: PUT /website/blog2/1 { "t ...
- nginx子配置文件实例
[root@bogon conf.d]# cat /etc/nginx/conf.d/test6.conf server { listen 8085; server_name 192.168.0.20 ...
- nginx查看并发数量
cat >> /etc/nginx/conf.d/status.conf << EOF server{ listen ; server_name www.test2.com; ...
- 更新操作 关于json字符串的拼接、json字符串与json对象之间的转换
更新操作 后台 /** * 更新人员 * @return "updateSdr" */ public String updateTheSdr(){ jsonstr = " ...