问题描述 LeetCode代码,使用了do while语句来减少判断分支

class Solution:
    def numEnclaves(self, grid: List[List[int]]) -> int:
        cells_num = 0
        cells_sum = 0
        to_check = set()
        directions = [(-1,0),(1,0),(0,1),(0,-1)]
        flag = 0

        for i in range(1, len(grid)-1):
            for j in range(1, len(grid[0])-1):
                if grid[i][j] == 1:
                    grid[i][j] = 0
                    cells_num += 1
                    ix = i
                    iy = j
                    while True:
                        for dx, dy in directions:
                            if 0 <= ix+dx < len(grid) and 0 <= iy+dy < len(grid[0]):
                                if grid[ix+dx][iy+dy] == 1:
                                    grid[ix+dx][iy+dy] = 0
                                    to_check.add((ix+dx, iy+dy))
                                    if ix + dx == 0 or ix + dx == len(grid) - 1 or iy + dy == 0 or iy + dy == len(grid[0]) - 1:
                                        flag = 1
                                    else:
                                        cells_num += 1
                        if not to_check:
                            if flag:
                                flag = 0
                            else:
                                cells_sum += cells_num
                            cells_num = 0
                            break
                        ix, iy = to_check.pop()
        return cells_sum

Leetcode运行结果,速度比上一题有所提升

1020. Number of Enclaves刷题笔记_while语句


pycharm代码

class Solution:
    def numEnclaves(self, grid):
        cells_num = 0
        cells_sum = 0
        to_check = set()
        directions = [(-1,0),(1,0),(0,1),(0,-1)]
        flag = 0

        for i in range(1, len(grid)-1):
            for j in range(1, len(grid[0])-1):
                if grid[i][j] == 1:
                    grid[i][j] = 0
                    cells_num += 1
                    ix = i
                    iy = j
                    while True:
                        for dx, dy in directions:
                            if 0 <= ix+dx < len(grid) and 0 <= iy+dy < len(grid[0]):
                                if grid[ix+dx][iy+dy] == 1:
                                    grid[ix+dx][iy+dy] = 0
                                    to_check.add((ix+dx, iy+dy))
                                    if ix + dx == 0 or ix + dx == len(grid) - 1 or iy + dy == 0 or iy + dy == len(grid[0]) - 1:
                                        flag = 1
                                    else:
                                        cells_num += 1
                        if not to_check:
                            if flag:
                                flag = 0
                            else:
                                cells_sum += cells_num
                            cells_num = 0
                            break
                        ix, iy = to_check.pop()
        return cells_sum

grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
print(Solution().numEnclaves(grid))

grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
print(Solution().numEnclaves(grid))

grid = [[0, 0, 0, 1, 1, 1, 0, 1, 0, 0], [1, 1, 0, 0, 0, 1, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1, 0, 1, 0, 0],
       [0, 1, 1, 0, 0, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 1, 1, 1, 0, 1],
       [0, 1, 1, 0, 0, 0, 1, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 1, 0, 0, 0, 1]]
print(Solution().numEnclaves(grid))