亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] Island Perimeter

robin / 1958人閱讀

Problem

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:

Solution
class Solution {
    public int islandPerimeter(int[][] grid) {
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    count += 4;
                    count -= findOnesAround(grid, i, j);
                }
            }
        }
        return count;
    }
    private int findOnesAround(int[][] grid, int i, int j) {
        int count = 0;
        if (i+1 < grid.length && grid[i+1][j] == 1) count++;
        if (i-1 >= 0 && grid[i-1][j] == 1) count++;
        if (j+1 < grid[0].length && grid[i][j+1] == 1) count++;
        if (j-1 >= 0 && grid[i][j-1] == 1) count++;
        return count;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/77097.html

相關(guān)文章

  • leetcode463. Island Perimeter

    摘要:要求計(jì)算出島嶼的周長。思路和代碼這題不難,直觀的來看,其實(shí)只要判斷出這一塊土地幾面臨海就知道需要加上幾條邊長。臨海的判斷有兩個(gè),一個(gè)是這塊地位于數(shù)組的邊緣,一個(gè)是這塊地相鄰的元素為,即海洋。代碼如下上方臨海左側(cè)臨海右側(cè)臨海下方臨海 題目要求 You are given a map in form of a two-dimensional integer grid where 1 rep...

    Raaabbit 評論0 收藏0
  • Leetcode PHP題解--D38 463. Island Perimeter

    摘要:題目鏈接題目分析給定一個(gè)二維數(shù)組,代表一個(gè)二維表格。代表有內(nèi)容,代表沒有。思路最簡單的辦法是,判斷當(dāng)前格子是否位,且上下左右是否為。當(dāng)都為時(shí),即當(dāng)前位置是單獨(dú)的一個(gè)格子,算上下左右共條邊。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 463. Island Perimeter 題目鏈接 463. Island Perimeter 題目分析 給定一個(gè)二維數(shù)組,代表一個(gè)二維表格。 里...

    xialong 評論0 收藏0
  • Leetcode PHP題解--D62 976. Largest Perimeter Triangl

    摘要:思路對給定的數(shù)組進(jìn)行降序排序,使最大的數(shù)字在前面。取最大的前三條,判斷任兩邊之和是否大于第三邊。是則返回周長即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 D62 976. Largest Perimeter Triangle 題目鏈接 976. Largest Perimeter Triangle 題目分析 給定數(shù)字?jǐn)?shù)組,任取三條邊形成三角形,返回最大邊長。 思路 對給定的數(shù)...

    GHOST_349178 評論0 收藏0
  • [LeetCode] 695. Max Area of Island

    Problem Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are s...

    dack 評論0 收藏0
  • leetcode 695 Max Area of Island

    摘要:返回注意答案并不是因?yàn)殛懙叵噙B要求必須是在上下左右四個(gè)方向。返回應(yīng)為想法我們還是要遍歷數(shù)組中的每一個(gè)元素。如果數(shù)組元素值為,則我們以這個(gè)值為起點(diǎn)進(jìn)行深度優(yōu)先搜索。 題目詳情 Given a non-empty 2D array grid of 0s and 1s, an island is a group of 1s (representing land) connected 4-di...

    PascalXie 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<