One pass python solution 89ms


  • 0
    B

    Quick one pass python solution. Can't see the distribution, took 89ms.

    class Solution(object):
        
        def isValidSudoku(obj, board):
    
            if len(board) != 9:
                return False
    
            for row in board:
                if len(row) != 9:
                    return False
    
            col_mask = []
            sqr_mask = []
            for i in range(9):
                col_mask.append(0)
                sqr_mask.append(0)
    
            for i in range(9):
                row = board[i]
                row_mask = 0
                for j in range(9):
                    if row[j] == '.':
                        continue
                    val = 1 << int(row[j])
    
                    board_x = int(j / 3)
                    board_y = int(i / 3)
                    sqr_index = board_x + 3 * board_y
    
                    if sqr_mask[sqr_index] & val != 0:
                        return False
                    if col_mask[j] & val != 0:
                        return False
                    if row_mask & val != 0:
                        return False
    
                    row_mask |= val
                    col_mask[j] |= val
                    sqr_mask[sqr_index] |= val
    
            return True
    

Log in to reply
 

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