LeetCode: 463 Island Perimeter(easy)
题目:
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: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
代码:
循环遍历就好了~
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int result = ;
int n = grid.size();
int m = grid[].size();
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(grid[i][j] == ){
if(i== || grid[i-][j] == ) result++;
if(i==n- || grid[i+][j] == ) result++;
if(j== || grid[i][j-] == ) result++;
if(j==m- || grid[i][j+] == ) result++;
}
}
}
return result;
}
};
LeetCode: 463 Island Perimeter(easy)的更多相关文章
- 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] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
随机推荐
- java 回文字符串
package string.string1_5; public class Palindrome { /** * 从两头向中间移动 * @param str * @return */ public ...
- 系统函数C字符串的实现(11):strchr
字符查找函数strchr char *mystrchr(const char *str, const char c) { char *p = NULL; for (char*newp = str; * ...
- eclipse中run as无run as server选项的解决方案
在项目->右击->Properties->Project Facets->Modify Project,选择Java和DynamicWeb Module
- spring 拦截器简介
spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...
- Android笔记之为自定义对话框添加移动动画效果
给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...
- java中两字符串比较--compareTo方法
java.lang.String.compareTo() 方法比较两个字符串的字典,比较是基于字符串中的每个字符的Unicode值 String n1 = "1"; String ...
- spring和springmvc中,Configuration注解Bean重复加载
问题:bean重复加载1.如下代码所示,开启Configuration注解,实现Bean代码注入,发现bean重复加载 @Configuration public class EhCacheConfi ...
- AmIBeingDebugged 函数方法的定义实现
#include <assert.h> #include <stdbool.h> #include <sys/types.h> #include <unist ...
- CSS控制文本的长度,超过一行显示省略号
代码如下: <div style="width:100px;height:20px;text-overflow:ellipsis; white-space:nowrap; overfl ...
- 详细阐述ping命令中请求超时与无法访问的区别
1.Request timed out 这是大家经常碰到的提示信息,很多文章中说这是对方机器置了过滤ICMP数据包,从上面工作过程来看,这是不完全 正确的,至少有下几种情况. (1) 对方已关机,或者 ...