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

例子:

[[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. 【转】Java操作CSV文件导入导出

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. 【数据算法】Java实现二叉树存储以及遍历

    二叉树在java中我们使用数组的形式保存原数据,这个数组作为二叉树的数据来源,后续对数组中的数据进行节点化操作. 步骤就是原数据:数组 节点化数据:定义 Node节点对象 存储节点对象:通过Linke ...

  3. RF Setup和Teardown的使用

    执行顺序 setup执行顺序是:project级别setup>suite级别setup>case级别setup 无返回值 Run Keywords | [KW1 | arg1 | arg2 ...

  4. ControlTemplate in WPF —— DatePicker

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  5. Nova 启动虚拟机流程解析

    目录 文章目录 目录 前言 从请求说起 nova-api service 阶段 前言 Nova 启动虚拟机的东西太多,持续更新- 从请求说起 无论是通过 Dashboard 还是 CLI 启动一个虚拟 ...

  6. 正确关闭selinux

    .查看当前selinux的状态命令为 getenforce .两个都要关.注意先看看有么有这两个文件,如果没有就创建一个,否则后期会出现很多问题 cat > /etc/selinux/confi ...

  7. 查询sq字段逗号分隔的方式

    2,3,4 -- select * from t_qs_anlycomagingconfig twhere and ( to_char(','||t.valid_month||',') like '% ...

  8. char* a = "abc" 和 char a[] = "abc" 之间的区别

    char* a = "abc"; 声明了一个字符类型的指针a,并为它赋值初始值为"abc",a的值是字符串"abc"的首地址[第一个字符的地 ...

  9. js 数组去重方法总结

    var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, ...

  10. [总集] LOJ 分块1 – 9

    目录 分块9题 出题人hzw的解析 数列分块入门 1 修改:区间加 查询:单点值查询 代码 数列分块入门 2 修改:区间加 查询:区间排名 代码 数列分块入门 6 修改:单点插入 查询:单点值 代码 ...