LeetCode#36. Effective Sudoku

Posted by crowezr on Thu, 10 Feb 2022 04:47:54 +0100

Title:

Please judge whether a Sudoku of {9 x 9 is effective. You only need to verify whether the numbers you have filled in are valid according to the following rules.

The numbers , 1-9 , can only appear once in each line.
The numbers ^ 1-9 ^ can only appear once in each column.
The numbers ^ 1-9 ^ can only appear once in each ^ 3x3 ^ uterus separated by a thick solid line. (please refer to the example diagram)
 

be careful:

An effective Sudoku (partially filled) is not necessarily solvable.
You only need to verify whether the filled numbers are valid according to the above rules.
Use '.' for blank spaces Indicates.
 

Example 1:


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
Example 2:

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: except that the first number in the first line is changed from 5 to 8, other numbers in the space are the same as example 1. However, due to the existence of two 8's in the 3x3 uterus in the upper left corner, this Sudoku is invalid.
 

Tips:

board.length == 9
board[i].length == 9
board[i][j] is a digit (1-9) or '.'

Source: LeetCode
Link: Force buckle

Personally, I think this question is mainly about the difficulty of the third condition of Sudoku judgment. The first two conditions should be able to understand the principle from the code. Let's mainly talk about the judgment process of the third condition.

Let's create a string first, which will be used in the following method.

store="0123456789"

Condition 1:

        for i in board:
            for j in store:
                num=i.count(j)
                if num>1:
                    return False

Condition 2:

        nums=0
        while nums < 9:
            ls=[]
            for i in board:
                ls.append(i[nums])
            for j in store:
                if ls.count(j)>1:
                    return False
            nums+=1

Condition 3:

We know that each Sudoku matrix has 9 small cube matrices, so we can think of a method to judge whether a small cube matrix is qualified each time. In this way, we have to judge a total of 9 times (that is, 9 cycles).

Then subdivide, every three small square matrices are in one row, so we can judge three times in each row (cycle 3 times).

In this way, with the same method as method 2 (establishing another list), we can judge whether each of our small squares is qualified.

Here, I created two variables to assist in the loop. line: indicates the number of rows indexed; lis: indicates the number of columns in the index.

        line=0;lis=0;ll=[]
        for i in range(3):  #Cycle three big lines (one big line corresponds to three small lines)
            for i in range(3):  #For each large line (three small lines), cycle three times, and 3 times 3 equals 9
                for j in board[line:line+3]:
                    for k in j[lis:lis+3]:
                        ll.append(k)
                for m in store:
                    if ll.count(m)>1:
                        return False
                lis+=3 #The number of columns advances three and enters the next cube matrix of the large row
                ll=[]  #Pay attention to clear the list after judging each small cube matrix
            lis=0    #After a large row is cycled, the number of columns returns to zero
            line+=3  #Go a long way

Finally, if the above judgment does not return False, then this is an effective Sudoku. We can return True at the end.

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        store="123456789"
        for i in board:
            for j in store:
                num=i.count(j)
                if num>1:
                    return False
        nums=0
        while nums < 9:
            ls=[]
            for i in board:
                ls.append(i[nums])
            for j in store:
                if ls.count(j)>1:
                    return False
            nums+=1
        line=0;lis=0;ll=[]
        for i in range(3):
            for i in range(3):
                for j in board[line:line+3]:
                    for k in j[lis:lis+3]:
                        ll.append(k)
                for m in store:
                    if ll.count(m)>1:
                        return False
                lis+=3 
                ll=[]
            lis=0
            line+=3
        return True

Topics: Algorithm leetcode