题目: 以二维数组形式表示坐标岛屿,求边长。

例子:

[[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的更多相关文章

  1. 【LeetCode】463. Island Perimeter 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

  2. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  3. 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  4. 【leetcode】976. Largest Perimeter Triangle

    题目如下: Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 463. Island Perimeter - LeetCode

    Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. 【python】小型神经网络的搭建

    import numpy as np def sigmoid(x): # Sigmoid activation function: f(x) = 1 / (1 + e^(-x)) return 1 / ...

  2. charts_03

    table 数值获取: 1.http://www.w3school.com.cn/jsref/dom_obj_all.asp 2.http://blog.csdn.net/xs_zgsc/articl ...

  3. 硬盘类型和Linux分区

    1. 硬盘类型和Linux分区 分类: Linux系统与应用 2011-11-22 20:54 1404人阅读 评论(0) 收藏 举报 linuxide扩展ftp服务器服务器redhat 真怪自己知识 ...

  4. DAY 4模拟赛

    DAY 4 zhx出题 T1 裂变链接 [问题描述] 你是能看到第一题的 friends 呢. ——hja 众所周知,小葱同学擅长计算,尤其擅长计算组合数,但这个题和组合数没什么关系. 现在有

  5. react-native 环境安装常见问题

    npm install react-native-cli -g react-native init yourproject npm install react-native run-ios 问题1:卡 ...

  6. Slider 滑块

    通过拖动滑块在一个固定区间内进行选择 ¶基础用法 在拖动滑块时,显示当前值 通过设置绑定值自定义滑块的初始值 <template> <div class="block&qu ...

  7. 阶段3 2.Spring_08.面向切面编程 AOP_2 spring中的aop术语和细节

    基于接口的动态代理要求,要去被代理对象最少实现一个接口 基于子类的动态代理没有实现接口的要求.但是要求我们的被代理类不能是最终类 在Spring中可以选择基于接口还是子类的动态代理 术语 大白话.业务 ...

  8. 解决kubeadm部署kubernetes集群镜像问题

    kubeadm 是kubernetes 的集群安装工具,能够快速安装kubernetes 集群.kubeadm init 命令默认使用的docker镜像仓库为k8s.gcr.io,国内无法直接访问,需 ...

  9. CSS 易混淆属性

    1. div中 height和line-height的区别 <div style="height:120px;">是用来规定整个div的高度,文字还是默认会在顶端开始向 ...

  10. django设置mysql为数据库笔记

    1,guest/settings.py中加上 import pymysql pymysql.install_as_MySQLdb() 安装好pymysql 2,guest/settings.py的DA ...