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

例子:

[[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. js将伪数组转换为标准数组的多种方法

    在js中,数组是特殊的对象,凡是对象有的性质,数组都有,数组表示有序数据的集合,而对象表示无序数据的集合. 那伪数组是什么呢,当然它也是对象,伪数组一般具有以下特点: 按索引方式存储数据: 具有len ...

  2. 作业要求20191010-9 alpha week 1/2 Scrum立会报告+燃尽图 07

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8752 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...

  3. Spark学习(一)——Spark运行架构

    基本概念 在具体讲解Spark运行架构之前,需要先了解几个重要的概念: RDD:是弹性分布式数据集(Resilient Distributed Dataset)的简称,是分布式内存的一个抽象概念,提供 ...

  4. watir学习系列--对话框处理(转)

    1.下面是网上编写的类库,保存为libAutoit.rb #LibAutoit主要处理windows弹出的对话框,调用autoit类进行处理 #函数如下: #- ChooseFileDialog函数: ...

  5. 1.zookeeper原理解析-数据存储之Zookeeper内存结构

    Zookeeper是怎么存储数据的,什么机制保证集群中数据是一致性,在网络异常,当机以及停电等异常情况下恢复数据的,我们知道数据库给我们提供了这些功能,其实zookeeper也实现了类似数据库的功能. ...

  6. nginx location正则

    nginx location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # ...

  7. js对数组分组处理

    一.js数组分组 1.js对数据分组类似group by 源码如下: <!DOCTYPE html> <html lang="en"> <head&g ...

  8. datagrid数据清空

    方法一: 不管是url方式还是加载本地数据的方式,均可以直接使用loadData方法清空数据,一行代码就可以清空: $('#tt').datagrid('loadData',{total:0,rows ...

  9. Fiddlercore拦截并修改HTTPS链接的网页,实现JS注入

    原始出处:https://www.cnblogs.com/Charltsing/p/FiddlerCoreHTTPS.html Fiddlercore可以拦截和修改http的网页内容,代码在百度很多. ...

  10. python3使用ltp语言云

    text="我爱自然语言处理." text=str(text) #text=urllib.quote(text) text=urllib.parse.quote(text) def ...