大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~
2021_iThome铁人赛
Leetcode
Determine if a 9 x 9
Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1-9
without repetition.1-9
without repetition.3 x 3
sub-boxes of the grid must contain the digits 1-9
without repetition.Note:
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
board.length == 9
board[i].length == 9
board[i][j]
is a digit 1-9
or '.'
.首先先简单的翻译一下题目
给一个数独题目要你解,给一个数独题目要判断这个数独题目是不是合乎数独的规则,也就是不管是行、列和9块3 x 3
的区块,出现的数字1-9
皆不能重复。
作法大致上是这样
for rowVal in board:
的方式,只是每个值会在额外配上一个sequence,这个sequence的起始可以透过start
参数来决定。Row
跟Column
来说明。row
和col
这两个list存着已经出现过的值,假如新读到的值已经有存在里头了,就代表重复了,回传False
;没有的话,就存入。subBox
这个list存着已经出现过的值,假如新读到的值已经有存在里头了,就代表重复了,回传False
;没有的话,就存入。0,0
、0,1
、0,2
1,0
、1,1
、1,2
2,0
、2,1
、2,2
subBox
的list判断有没有重复数字了。class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
row = []
col = []
subBox = []
for seq1, rowVal in enumerate(board, start=0):
for seq2, colVal in enumerate(rowVal, start=0):
if colVal is not ".":
# Row part
if (seq1, colVal) not in row:
row.append((seq1, colVal))
else:
return False
# Column part
if (seq2, colVal) not in col:
col.append((seq2, colVal))
else:
return False
# subBox part
if (int(seq1/3), int(seq2/3), colVal) not in subBox:
subBox.append((int(seq1/3), int(seq2/3), colVal))
else:
return False
return True
Python
大家明天见
语法拼接 前面Injection的爱恨情仇4讲到SQL injection常发生在语法拼接的地方,这...
大多数普通媒体播放器都无法播放蓝光ISO档、蓝光光盘以及BD资料夹。在这篇文章中,我将重点介绍市场上...
0x1 前言 目前还没有画面可以看或操作,所以单就资料流的部分先写进资料库, 而建立订单目前也是先塞...
写了好几天的事前准备、我想大家应该都腻了。终於、准备到了一定程度、可以进入本文了。这篇文章主要的目的...
今天心情蛮好的,期待叻2周终於等到这天了,生平第一次染发:) 这篇文有一半是我边染发变打出来的~ 染...