码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode#36 Valid Sudoku

时间:2015-07-24 22:06:51      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

Problem Definition:

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.

Solution: 用仨字典,一次扫描,针对n x n 的Sudoku,时间复杂度O(n^2).

 1 def isValidSudoku( board ):
 2         col,row,block={},{},{}
 3         for i in range(9):
 4             for j in range(9):
 5                 if board[i][j]==.:
 6                     continue
 7                 d=board[i][j]
 8                 T=row.get(str(i)+d,False)
 9                 if T:
10                     return False
11                 else:
12                     row[str(i)+d]=True
13                 T=col.get(str(j)+d,False)
14                 if T:
15                     return False
16                 else:
17                     col[str(j)+d]=True
18                 T=block.get(str((i/3)*3+j/3)+d,False)
19                 if T:
20                     return False
21                 else:
22                     block[str((i/3)*3+j/3)+d]=True
23         return True

 

 

 
 

LeetCode#36 Valid Sudoku

标签:

原文地址:http://www.cnblogs.com/acetseng/p/4674443.html

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