1. 程式人生 > >Python實現"島嶼的周長 "的三種方法

Python實現"島嶼的周長 "的三種方法

給定一個二維整數的網格地圖,網格中1代表陸地,0代表水域。網格單元的連線方式是水平和垂直。網格完全被水域包圍,其中有一個島嶼。這個島嶼內部沒有水域。每個單元格是長度為1的正方形。網格本身是長和寬不超過100的矩陣。請確定這個島嶼的周長 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、本題目標是求如上圖中的深色區域的周長,難點在於需要判斷每一個單元格是否與矩陣的周長重疊。為了解決這個問題,可以將矩陣向四周擴充一個單元格,擴充的單元格值為1,將長和寬分別加1(4 x 5的矩陣 -> 5 x 6的矩陣)。

def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if not grid or not grid[0]:
            return 0
        
        premeter = 0
        height = len(grid)    #擴充前矩陣的高和寬
        width = len(grid[0])
        
        for i in range(height)
: #矩陣擴充 grid[i].insert(0, 0) grid[i].append(0) upAndDown = [0] * (width + 2) grid.insert(0, upAndDown) grid.append(upAndDown) for i in range(1, height + 1): for j in range(1, width + 1): if grid[i][j] == 1: #計算島嶼周長
if grid[i][j - 1] == 0: premeter += 1 if grid[i][j + 1] == 0: premeter += 1 if grid[i - 1][j] == 0: premeter += 1 if grid[i + 1][j] == 0: premeter += 1 return premeter

優雅寫法(參考他人)

def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        grid = [[0] * len(grid[0])] + grid + [[0] * len(grid[0])]
        grid = [[0] + g + [0] for g in grid]
        sum = 0
        for i in range(1, len(grid) - 1):
            for j in range(1, len(grid[0]) - 1):
                if grid[i][j] == 1:
                    a = grid[i - 1][j] == 1
                    b = grid[i + 1][j] == 1
                    c = grid[i][j - 1] == 1
                    d = grid[i][j + 1] == 1
                    sum += 4 - (a + b + c + d)
        return sum

2、不擴充矩陣,用if語句判斷邊界

def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        premeter=0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j]==1:
                    if i==0:
                        premeter+=1
                    elif grid[i-1][j]==0:
                        premeter+=1
                    if i==(len(grid)-1):
                        premeter+=1
                    elif grid[i+1][j]==0:
                        premeter+=1
                    if j==0:
                        premeter+=1
                    elif grid[i][j-1]==0:
                        premeter+=1
                    if j==(len(grid[0])-1):
                        premeter+=1
                    elif grid[i][j+1]==0:
                        premeter+=1
        return premeter

3、除卻第一行和第一列,判斷每一個單元格。如果當前單元格[i,j]代表陸地,則周長加4。如果該單元格[i ,j]正上方單元格[i-1 ,j]代表陸地,則周長減2。如果該單元格[i ,j]左邊單元格[i, j-1]代表陸地,則周長減2。(參考他人)

def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        perimeter = 0
        for i in range(len(grid)):
            for j in range(len(grid[i])):
                if grid[i][j] == 1:
                    perimeter += 4
                    if i - 1 >= 0 and grid[i - 1][j] == 1:
                        perimeter -= 2
                    if j - 1 >= 0 and grid[i][j - 1] == 1:
                        perimeter -= 2
        return perimeter

演算法題來自:https://leetcode-cn.com/problems/island-perimeter/description/