Leetcode演算法 36 判斷有效數獨

2021-08-31 09:28:59 字數 4674 閱讀 8286

判斷乙個 9*9 的數獨面板是否是有效的。

如果已經被填充的數字滿足以下條件,則說明是有效的:

備註:example 1:

input:

[[「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

example 2:

input:

[[「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.

按照數獨的有效性定義,需要從行、列、3*3子塊三個方面都滿足數字的唯一性。

因此,可以分三個步驟,分別檢驗三個條件,只要有乙個條件不滿足,則說明數獨無效。

1、檢驗每一行是否有重複數字。可以定義乙個set,存放掃瞄過的數字,一旦新數字已經被包含在set中,則說明重複。

2、檢驗每一列是否有重複數字。與行同理。

3、檢驗每乙個子塊是否有重複數字。需要檢查9個子塊。

上述方法是分別檢驗三個條件,每個條件都需要遍歷一遍所有的數字。

實際上,可以只通過一次遍歷,來同時判斷是否滿足三個條件。

維護乙個 set,這個set中存放三種格式的元素:

由於元組長度以及元素型別的不同,三種元素不會相互混合。

因此,每次掃瞄到乙個數字,便生成 3 個對應的元組,分別判斷是否存在於 set 中,如果存在,則說明數獨無效;否則,將 3 個元組都加入到 set 中,繼續掃瞄下乙個數字。

def

isvalidsudoku

(board)

:"""

:type board: list[list[str]]

:rtype: bool

依次判斷3個有效條件是否全部滿足。

"""defis_row_valid

(board)

:'''

判斷乙個數獨的每一行是否都包含無重複數字1-9

'''for row in board:

char_set =

set(

)for char in row:

if char !=

'.':

if char in char_set:

return

false

char_set.add(char)

return

true

# 1、每一行一定要包含無重複數字1-9

ifnot is_row_valid(board)

:return

false

# 2、每一列一定要包含無重複數字1-9

board_reverse =

[[board[i]

[j]for i in

range(9

)]for j in

range(9

)]ifnot is_row_valid(board_reverse)

:return

false

# 3、每乙個 3*3 的子面板一定要包含無重複數字1-9

for i in

range(3

):for j in

range(3

):char_set =

set(

)for ii in

range

(i*3

,(i+1)

*3):

for jj in

range

(j*3

,(j+1)

*3):

char = board[ii]

[jj]

if char !=

'.':

if char in char_set:

return

false

char_set.add(char)

return

true

defisvalidsudoku2

(board)

:"""

:type board: list[list[str]]

:rtype: bool

一次遍歷,同時判斷是否滿足3個有效條件。

"""char_set =

set(

)for i in

range(9

):for j in

range(9

):char = board[i]

[j]if char !=

'.':

if(i, char)

in char_set \

or(char, j)

in char_set \

or(i//

3, j//

3, char)

in char_set:

return

false

else

: char_set.add(

(i, char)

)# 第i行的char字元

char_set.add(

(char, j)

)# 第j列的char字元

# [i//3, j//3]位置的小面板的char字元

char_set.add(

(i//

3, j//

3, char)

)return

true

if'__main__'

== __name__:

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"]

]print

(isvalidsudoku2(board)

)

Leetcode 判斷有環鏈表的環長度 2

思路 當兩個指標相遇,證明鍊錶有環的時候,讓兩個指標從相遇點繼續迴圈前進,並統計前進的迴圈次數,直到兩個指標第2次相遇,此時,統計出來的前進次數就是環數。思考一下 假設兩指標分為為p1,p2,則指標p2每次比p1多走一步,兩者的速度差為1步,當兩個指標再次相遇,p2比p1多走了1圈,因此 環長 每一...

質數判斷(你有珠心算,我有珠心算法)

看過 最強大腦 的小夥伴應該都知道珠心算。尤其是在中國隊對陣日本隊時,那種緊張的氣氛婉如世紀大戰一觸即發。對於大多數和我一樣的普通小老百姓而言就只剩下感嘆的份了。所謂的珠心算其實就是 珠算 和 心算 的結合體。乙個龐大的數字在瞬間就能分辨是否是質數,這種計算量要是讓我來,怎麼也得廢上好幾本筆記本了。...

Bellman Ford判斷有負環的演算法模板

include include include include include include include include include includeusing namespace std typedef long long ll const int int max 0x07777777 c...