Leetcode 463. Island Perimeter Java language

Source: Internet
Author: User

You is given a map in form of a two-dimensional an integer grid where 1 represents land and 0 represents water. Grid cells is connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there are exactly one island (i.e., one or more connected land cells). The island doesn ' t has "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:16explanation:the perimeter is the yellow stripes in the image below:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/8C/9E/wKiom1hyR86g3TraAAAHzYGxJlk325.png-wh_500x0-wm_3 -wmp_4-s_1369936785.png "title=" Island.png "alt=" Wkiom1hyr86g3traaaahzygxjlk325.png-wh_50 "/>

Test instructions: Find the perimeter of the island.

Given a square map of two-dimensional integers, 1 represents land, and 0 represents a place of water. Each square is vertically or horizontally connected, without a slash. Surrounded by water is a small island (the island may contain a lot of land squares), there is no Island lake in the island (internal water and external water is connected). The side length of each square unit is 1. Each place is at right angles, the length of the width of not more than 100, judging the side length of the island.

Ideas

To determine whether there is land on the four sides, there is no contribution to the perimeter of the land, the contribution of land to the perimeter of 2, one side of the contribution of the circumference of the land of 3, four-sided no land contribution circumference of 4 ... There are ideas, but I can't even write ... Metrorrhagia, to see for a while to understand.

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/8C/9B/wKioL1hySKnwBr9lAAWTNR9dMh8045.jpg-wh_500x0-wm_3 -wmp_4-s_283724932.jpg "title=" img_20170108_220537.jpg "alt=" Wkiol1hysknwbr9laawtnr9dmh8045.jpg-wh_50 "/>

This is also the reason the code if is determined to write this. Normal situation when we traverse to an island, we have to judge the situation of the surrounding four neighbors, but when there are four cases, some of the neighbors are out of bounds and not judged, so use | | To control, the front is true when the back directly without looking, short-circuit. Without prejudice to other circumstances.

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++;    &nbsP;           if (j==n-1| | grid[i][j+1]==0)  sum++;                 }        return sum;    }}


Leetcode 463. Island Perimeter Java language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.