Very clear Python; One pass; Bit manipulation to save space


  • 0
        def isValidSudoku(self, board):
            k = 9
            row = [0] * k
            col = [0] * k
            cube = [[0] * 3 for _ in range(3)]
            for i in range(k):
                for j in range(k):
                    if board[i][j] != '.':
                        tmp = 1 << int(board[i][j])
                        if tmp & row[i] or tmp & col[j] or tmp & cube[i / 3][j / 3]:
                            return False
                        row[i] |= tmp
                        col[j] |= tmp
                        cube[i / 3][j / 3] |= tmp
            return True
    

Log in to reply
 

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