Posted on 2022-10-29 18:42
Uriel 阅读(59)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
判断给定的9*9的数(或者‘.’)是否是正确的数独状态,直接判,用python dict(所以为啥是Medium难度捏?
1 #36
2 #Runtime: 74 ms
3 #Memory Usage: 13.6 MB
4
5 class Solution(object):
6 def isValidSudoku(self, board):
7 """
8 :type board: List[List[str]]
9 :rtype: bool
10 """
11 for i in range(9):
12 fg1 = {}
13 for j in range(9):
14 if board[i][j] != '.' and board[i][j] in fg1:
15 return False
16 fg1[board[i][j]] = 1
17 for i in range(9):
18 fg2 = {}
19 for j in range(9):
20 if board[j][i] != '.' and board[j][i] in fg2:
21 return False
22 fg2[board[j][i]] = 1
23 for i in range(0, 9, 3):
24 for j in range(0, 9, 3):
25 fg = {}
26 for ii in range(i, i + 3):
27 for jj in range(j, j + 3):
28 if board[ii][jj] != '.' and board[ii][jj] in fg:
29 return False
30 fg[board[ii][jj]] = 1
31 return True