title: leetcode no.36
categories:
tags:
判斷乙個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。
數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每乙個以粗實線分隔的 3x3 宮內只能出現一次。
上圖是乙個部分填充的有效的數獨。
數獨部分空格內已填入了數字,空白格用 『.』 表示。
示例 1:
輸入:[
["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"]
]輸出: true
示例 2:
輸入:[
["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"]
]輸出: false
解釋: 除了第一行的第乙個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:乙個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字 1-9 和字元 '.' 。
給定數獨永遠是 9x9 形式的。
核心思想:
實際就是乙個大模擬
分別進行如下判斷如果有不滿足的情況則結束判斷,最壞時間複雜度情況就是判斷全部情況
1. 判斷每行
2. 判斷每列
3. 判斷每個格仔
針對每行每列可以採用去掉所有"."然後set判斷長度是否一致
"""nums = list(str(i) for i in range(1,10))
# dictlist = dict(zip(nums,[0 for i in range(10)]))
# print(dictlist)
# 判斷每行
for i in range(9):
dictlist = dict(zip(nums,[0 for i in range(10)]))
for j in range(9):
if board[i][j] == '.':
continue
dictlist[board[i][j]] += 1
if dictlist[board[i][j]] >= 2:
return false
# 判斷每列
for i in range(9):
dictlist = dict(zip(nums,[0 for i in range(10)]))
for j in range(9):
if board[j][i] == '.':
continue
dictlist[board[j][i]] += 1
if dictlist[board[j][i]] >= 2:
return false
# 判斷每個格仔
for i in range(9):
dictlist = dict(zip(nums, [0 for i in range(10)]))
remainder = i % 3 # 餘數
consult = int(i / 3) # 商
for j in range(3):
for k in range(3):
if board[j+consult*3][k+remainder*3] == '.':
continue
dictlist[board[j+consult*3][k+remainder*3]] += 1
if dictlist[board[j+consult*3][k+remainder*3]] >= 2:
return false
return true
if __name__ == '__main__':
s = solution()
print(s.isvalidsudoku([
["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","5"]
]))
LeetCode第三十六題 判斷九宮格是否有效
valid sudoku 問題簡介 確定9x9數獨格是否有效,只需要根據以下規則驗證已填充的單元格 1.每行必須包含數字1 9而不重複 2.每列必須包含數字1 9而不重複 3.網格的9個3x3子框中的每乙個必須包含數字1 9而不重複 注 1.數獨板 部分填充 可能有效,但不一定是可解決的 2.只需要...
第三十六章 神女
滕青山也看著那馬背上的紫袍男子。這人似乎對我有著一絲敵意?滕青山心中倒是不在乎。在先天金丹這一層次。滕青山今有信心應付任何乙個強。你是第五神將?一道清冷聲音響起。滕青山身側。一身色裘衣的李然而立。目光清冷看著那微胖男子。不知道第五神將。來我這有什麼事?滕青山聽了心存疑。神將是什級別高手?第五神將 俄...
第三十六象 己亥
讖 曰纖纖女子 赤手禦敵 不分禍福 燈光蔽日 頌 曰雙拳旋轉乾坤 海內無瑞不靖 母子不分先後 西望長安入覲 金聖嘆註解 此象疑一女子能定中原,建都長安。推背圖網解 解圖圖中有一宮裝女子提燈在前指引,一婦人騎馬隨後,地上有一人納頭拜倒,從卦圖上看,有人點燈引路,婦人騎馬,應該是要出行,旁邊有人跪拜,證...