Advertisement
jinhuang1102

36. Valid Sudoku

Dec 12th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. class Solution:
  2.     def isValidSudoku(self, board):
  3.         """
  4.        :type board: List[List[str]]
  5.        :rtype: bool
  6.        """              
  7.         row = {}
  8.         for i in range(9):
  9.             row[i] = set()
  10.        
  11.         colos = {}
  12.         for i in range(9):
  13.             colos[i] = set()
  14.            
  15.         squares = {}
  16.         for i in range(3):
  17.             squares[i] = {}
  18.             for j in range(3):
  19.                 squares[i][j] = set()
  20.        
  21.         for i in range (len(board)):
  22.             for j in range(len(board[0])):
  23.                 num = board[i][j]
  24.                 if num != '.':
  25.                     if num in row[i]:
  26.                         return False
  27.                     else:
  28.                         row[i].add(num)
  29.                        
  30.                     if num in colos[j]:
  31.                         return False
  32.                     else:
  33.                         colos[j].add(num)
  34.                        
  35.                     if num in squares[i//3][j//3]:
  36.                         return False
  37.                     else:
  38.                         squares[i//3][j//3].add(num)
  39.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement