straightforward Python solution with a 3*3 array


  • 0
    T
    class Solution(object):
        def isValidSudoku(self, board):
            """
            :type board: List[List[str]]
            :rtype: bool
            """
            #check row
            for i in board:
            	tmp=[]
            	for j in i:
            		if j in tmp:
            			return False
            		if j!='.':
            			tmp.append(j)
            #check col
            for i in range(9):
            	tmp=[]
            	for j in board:
            		if j[i] in tmp:
            			return False
            		if j[i]!='.':
            			tmp.append(j[i])
            #check cell
            tmp=[[[],[],[]],[[],[],[]],[[],[],[]]]
            for i in range(9):
            	for j in range(9):
            		if board[i][j]=='.':
            			continue
            		elif board[i][j] in tmp[i/3][j/3]:
            			return False
            		else:
            			tmp[i/3][j/3].append(board[i][j])
            return True
    

Log in to reply
 

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