码迷,mamicode.com
首页 > 编程语言 > 详细

[leetcode]Valid Sudoku @ Python

时间:2014-06-07 11:26:18      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

原题地址:https://oj.leetcode.com/problems/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 ‘.‘.

bubuko.com,布布扣

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.

解题思路:判断是否为合法的数独。

代码:

bubuko.com,布布扣
class Solution:
    # @param board, a 9x9 2D array
    # @return a boolean
    def isValidSudoku(self, board):
        def isValid(x, y, tmp):
            for i in range(9):
                if board[i][y]==tmp:return False
            for i in range(9):
                if board[x][i]==tmp:return False
            for i in range(3):
                for j in range(3):
                    if board[(x/3)*3+i][(y/3)*3+j]==tmp: return False
            return True
        for i in range(9):
            for j in range(9):
                if board[i][j]==.:continue
                tmp=board[i][j]
                board[i][j]=D
                if isValid(i,j,tmp)==False: return False
                else:
                    board[i][j]=tmp
        return True
bubuko.com,布布扣

 

 

[leetcode]Valid Sudoku @ Python,布布扣,bubuko.com

[leetcode]Valid Sudoku @ Python

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/zuoyuan/p/3770184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!