【leetcode】Valid Sudoku

题目简述:

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

解题思路:

把三个都判断下就完了

class Solution:
    # @param board, a 9x9 2D array
    # @return a boolean
    def isValidSudoku(self, board):
        #row
        for i in range(9):
            vis = [False] * 9
            for j in range(9):
                if board[i][j] == '.':
                    continue
                if vis[int(board[i][j])-1] == True:
                    return False
                else:
                    vis[int(board[i][j])-1] = True
        
        #colum
        for i in range(9):
            vis = [False] * 9
            for j in range(9):
                if board[j][i] == '.':
                    continue
                if vis[int(board[j][i])-1] == True:
                    return False
                else:
                    vis[int(board[j][i])-1] = True
                    
        #block
        for i in range(0,9,3):
            for j in range(0,9,3):
                vis = [False] * 9
                for k in range(i,3+i):
                    for l in range(j,3+j):
                        if board[k][l] == '.':
                            continue
                        if vis[int(board[k][l])-1] == True:
                            return False
                        else:
                            vis[int(board[k][l])-1] = True
                            
        return True
posted @ 2015-04-18 23:43  mrbean  阅读(295)  评论(0编辑  收藏  举报