首页 > 其他分享 >leetcode-627-easy

leetcode-627-easy

时间:2023-01-03 21:45:44浏览次数:36  
标签:627 bigGrid int ++ length grid result easy leetcode

Island Perimeter

You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 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", meaning the water inside 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 1:


Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:

Input: grid = [[1]]
Output: 4
Example 3:

Input: grid = [[1,0]]
Output: 4
Constraints:

row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j] is 0 or 1.
There is exactly one island in grid.

思路一:给定的数组,长度为 i * j,可以把数组放在一个 (i+2) * (j+2) 的新数组内,遍历新数组的时候可以省去最外层边的判断

    public int islandPerimeter(int[][] grid) {
        int colLen = grid.length;
        int rowLen = grid[0].length;
        int[][] bigGrid = new int[colLen + 2][rowLen + 2];

        for (int i = 0; i < colLen; i++) {
            for (int j = 0; j < rowLen; j++) {
                bigGrid[i + 1][j + 1] = grid[i][j];
            }
        }

        int result = 0;
        for (int i = 1; i < bigGrid.length - 1; i++) {
            for (int j = 1; j < bigGrid[0].length - 1; j++) {
                result += checkCell(bigGrid, i, j);
            }
        }

        return result;
    }

    private static int checkCell(int[][] grid, int i, int j) {
        int result = 0;
        int cell = grid[i][j];
        if (cell == 1) {
            if (grid[i - 1][j] == 0) {
                result++;
            }

            if (grid[i + 1][j] == 0) {
                result++;
            }

            if (grid[i][j - 1] == 0) {
                result++;
            }

            if (grid[i][j + 1] == 0) {
                result++;
            }
        }
        return result;
    }

思路二:看了一下题解,发现有更好的解法,关注前面遍历过得方格,如果之前有相邻方格,就-2;

if (grid[i][j] == 1) {
    rsp += 4;
    if (i > 0 && grid[i - 1][j] == 1) {
        rsp -= 2;
    }
    if (j > 0 && grid[i][j - 1] == 1) {
        rsp -= 2;
     }
}

标签:627,bigGrid,int,++,length,grid,result,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/17023448.html

相关文章

  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • leetcode-441-easy
    ArrangingCoinsYouhavencoinsandyouwanttobuildastaircasewiththesecoins.Thestaircaseconsistsofkrowswheretheithrowhasexactlyicoins.Th......
  • leetcode-459-easy
    RepeatedSubstringPatternGivenastrings,checkifitcanbeconstructedbytakingasubstringofitandappendingmultiplecopiesofthesubstringtogether......
  • leetcode-492-easy
    ConstructtheRectangleAwebdeveloperneedstoknowhowtodesignawebpage'ssize.So,givenaspecificrectangularwebpage’sarea,yourjobbynowisto......
  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......
  • 【队列】LeetCode 232. 用栈实现队列
    题目链接232.用栈实现队列思路设置一个主栈mainStack和一个辅助栈assistantStack,在进行入队的时候,将mainStack中的元素全部放入assistantStack中,再将x入队,然......
  • [LeetCode] 1325. Delete Leaves With a Given Value 删除给定值的叶子结点
    Givenabinarytree root andaninteger target,deleteallthe leafnodes withvalue target.Notethatonceyoudeletealeafnodewithvalue target, ......
  • 【队列】LeetCode 225. 用队列实现栈
    题目链接225.用队列实现栈思路设置一个主队列mainQueue和一个辅助队列assistantQueue,在进行压栈的时候,将mainQueue中的元素全部放入assistantQueue中,再将x压......
  • Leetcode[LeetCode]4 两个有序数组的中位数
    上图为剑指Offer之字符串的排列,基于回溯法的思想。简单算法01数组中第二大的数02合并排序链表03链表反转04判断链表环05两个链表的首个交点06数组中出现大与一般的数07手写......