Scribbling

LeetCode: 73. Set Matrix Zeroes 본문

Computer Science/Coding Test

LeetCode: 73. Set Matrix Zeroes

focalpoint 2021. 9. 28. 20:28
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m, n = len(matrix), len(matrix[0])

        for y in range(m):
            for x in range(n):
                if matrix[y][x] == 0:
                    self.inspect_row(matrix, y, x)
                    self.inspect_column(matrix, y, x)
                elif matrix[y][x] == 'row inspected zero':
                    self.inspect_column(matrix, y, x)
                elif matrix[y][x] == 'column inspected zero':
                    self.inspect_row(matrix, y, x)
                else:
                    continue
        for y in range(m):
            for x in range(n):
                if type(matrix[y][x]) != int:
                    matrix[y][x] = 0

    def inspect_row(self, matrix, y, x):
        for i in range(len(matrix[0])):
            cur = matrix[y][i]
            if cur == 0:
                matrix[y][i] = 'row inspected zero'
            elif cur == 'row inspected zero':
                continue
            elif cur == 'column inspected zero':
                matrix[y][i] = 'both inspected zero'
            elif cur == 'both inspected zero':
                continue
            else:
                matrix[y][i] = 'to be zero'

    def inspect_column(self, matrix, y, x):
        for i in range(len(matrix)):
            cur = matrix[i][x]
            if cur == 0:
                matrix[i][x] = 'column inspected zero'
            elif cur == 'row inspected zero':
                matrix[i][x] = 'both inspected zero'
            elif cur == 'column inspected zero':
                continue
            elif cur == 'both inspected zero':
                continue
            else:
                matrix[i][x] = 'to be zero'

'Computer Science > Coding Test' 카테고리의 다른 글

LeetCode: 75. Sort Colors  (0) 2021.09.28
LeetCode: 74. Search a 2D Matrix  (0) 2021.09.28
LeetCode: 72. Edit Distance  (0) 2021.09.26
LeetCode: 71. Simplify Path  (0) 2021.09.26
LeetCode: 69. Sqrt(x)  (0) 2021.09.26