Leetcode 463. Island Perimeter JAVA语言

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:

题意:求岛的周长。

给定一个二维整数的方格地图,1代表陆地,0代表有水的地方。每个方格都是垂直或者水平连接的,没有斜线。被水包围的中间有一个小岛(小岛可能包含了很多个的陆地方格),小岛里没有岛中湖(内部的水和外部的水是连通的)。每个方格单元的边长都是1。各个地方都是成直角的,长度宽度不超过100,判断小岛的边长。

思路

判断其四面是否有陆地,四面都有陆地的对周长无贡献,两面有陆地的贡献周长为2,一面有陆地的贡献周长为3,四面都没有陆地的贡献周长为4。。。。。。有思路,但是竟然不会写。。。。。。。。。。。血崩,看了一会才想明白。

这也是代码if判断这样写的原因。正常情况当我们遍历到一座岛的时候我们要判断周围四个邻居的情况,但是当出现这四种情况时,部分邻居越界,不用判断,所以用||来控制,前面为真时后面就直接不看了,短路。而又不影响其他情况。

public class Solution {
    public int islandPerimeter(int[][] grid) {
        int sum=0;
        int m=grid.length,n=grid[0].length;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                if(grid[i][j]==1){ 
                if(i==0||grid[i-1][j]==0) sum++;
                if(i==m-1||grid[i+1][j]==0) sum++;
                if(j==0||grid[i][j-1]==0) sum++;
                if(j==n-1||grid[i][j+1]==0) sum++;
                }
        return sum;
    }
}
时间: 2024-08-24 10:38:18

Leetcode 463. Island Perimeter JAVA语言的相关文章

[LeetCode] 463. Island Perimeter 岛的周长

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 isla

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

LeetCode 463 Island Perimeter 解题报告

题目要求 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

463. Island Perimeter - LeetCode

Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2 一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1) 遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1 Java实现: public int islandPerimeter(int[][] grid) { int total = 0

463 Island Perimeter

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 isla

Leetcode 383. Ransom Note JAVA语言

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the 

Leetcode 263. Ugly Number JAVA语言

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

Leetcode 155. Min Stack JAVA语言

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

Leetcode 461. Hamming Distance JAVA语言

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. PS:求海明距离. 思路:就是求x和y二进制的异或中的1的个数 public class Solution {     public int ham