标签:
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.
Subscribe to see which companies asked this question
解题分析:
此题目的意思是判断给出的数独是否是合法的,那么根据题意,可以分成三步1、判断每一列是不是对的,判断每一行是不是对的,
判断九宫格是不是对的,
三个判断模块解决问题
# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def isValidSudoku(self, board):
#列判断
i = 0
while i < 9:
j = 0
d = {}
while j < 9:
if board[i][j] != '.' and board[i][j] in d:
return False
else:
d[board[i][j]] = True
j += 1
i += 1
# 行判断
j = 0
while j < 9:
i = 0
d = {}
while i < 9:
if board[i][j] != '.' and board[i][j] in d:
return False
else:
d[board[i][j]] = True
i += 1
j += 1
#九宫格判断
i = 0
while i < 9:
j = 0
while j < 9:
m = 0
d = {}
while m < 3:
n = 0
while n < 3:
if board[i + m][j + n] != '.' and board[i + m][j + n] in d:
return False
else:
d[board[i + m][j + n]] = True
n += 1
m += 1
j += 3
i += 3
return True
(LeetCode)Valid Sudoku --- 合法数独
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/52288467