Shard my python solution (O(n2) time cost)


  • 0
    W

    The following is my solution which use greedy method.

    class Solution:
        # @param board, a 9x9 2D array
        # @return a boolean
        def isValidSudoku(self, board):
            if not board:
                return False
    
            # row
            for i in range(len(board)):
                temp = []
                for j in board[i]:
                    if j in temp:
                        return False
                    elif j != '.':
                        temp.append(j)
    
            # column
            for i in range(len(board)):
                temp = []
                for j in range(len(board)):
                    element = board[j][i]
                    if element in temp:
                        return False
                    elif element != '.':
                        temp.append(element)
    
            # block
            block = 9
            loop = 3
            for index in range(block):
                row = index % 3 * 3
                column = index // 3 * 3
                temp = []
                for i in range(loop):
                    for j in range(loop):
                        element = board[row + i][column + j]
                        if element in temp:
                            return False
                        elif element != '.':
                            temp.append(element)
    
            return True

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.