Leetcode_36 effective [Sudoku]

Article Directory:

  • topic
  • A script
  • A script logic
  • Scripts two
  • Scripts two logic
  • shell solution Share

 


 

topic:

Determining a number of unique 9x9 is valid. Only according to the following rules, to verify whether the numbers already filled can be effective.

The numbers 1-9 appear only once in each row.
The numbers 1-9 appear only once in each column.
Figures 1-9 only appear once in each 3x3 intrauterine separated by a thick solid line.

The figure is the number of valid only a partially filled.

Sudoku part of this space has been filled in the numbers, empty cells with '.' Representation.

Example 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:".", "7", "9"] ] Output: true Example 2:".", "7", "9"] ] Output: true Example 2:

输入:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6 "], Description :However, due to the upper left corner of the 3x3 two intrauterine 8 exist, so that only the number is invalid.interpretation: in addition to the first number to the first row 58 from outside, are the same as in example 1 other digital space.Output: false]  [" "," "," "," "," 8 "," "," "," 7 "," 9 "]......Description :However, due to the upper left corner of the 3x3 two intrauterine 8 exist, so that only the number is invalid.interpretation: in addition to the first number to the first row 58 from outside, are the same as in example 1 other digital space.Output: false][" "," "," "," "," 8 "," "," "," 7 "," 9 "]......explanation:but As the upper-left corner of the palace has two 3x3 8 exist, so this number is only validinterpretation: the first number in addition to the first row 58 from outside, are the same as example 1 other digital this space.output: false][ ".", ".", ".", ".", "8" ,,, "7", "9"] "." "."  [". ",". " , ".", "4", "1", "9", ".", ".", "5"],
  [". "," 6 ",". ",". ",". ",". "," 2 "," 8 ",". "],






Only a valid number (portion has been filled) is not necessarily solvable.
According to the above rules only need to verify whether the figures have been filled can be effective.
Given the number of unique sequence comprises only numeric characters 1-9 and '.'.
Sudoku will always be given in the form of 9x9.


A script: [when using: 152ms]

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        n1,n2,n3,n4 = 0,0,0,0
        num1,num2 = 0,0
        str2,str3,str4 = "","",""
        def test_str(strr):
            n1 = strr.count(".")
            n2 = len(set(strr))
            if (9 - n1) != (n2 -1):
                return ( " to false " )
             the else : 
                return ( " to true " ) 
        # Check each row 
        for I in Range ( . 9 ): 
            N1 = Board [I] .count ( ' . ' ) 
            N2 = len (SET (Board [I]) )
             IF ( . 9 - N1) = (N2 -! . 1 ): 
                return (False) 
        # check each column 
        for I in Range ( . 9  ):
            str1= ""
            for j in range(9):
                str1 += board[j][i]
            result = test_str(str1)
            if result == "false":
                return(False)
            else:
                pass
        #检查小正方形
        for i in range(9):
            for j in range(9):
                if j < 3:
                    str2 += board[i][j]
                elif 2 < j < 6:
                    str3 += board[i][j]
                else:
                    str4 += board[i][j]
            if (i+1) % 3 == 0:
                result1 = test_str(str2)
                result2 = test_str(str3)
                result3 = test_str(str4)
                if result1 == "false" or result2 == "false" or result3 == "false":
                    return(False)
                else:
                    str2 = ""
                    str3 = ""
                    str4 = ""
        return(True)

A script logic:

  • First: how to determine the numbers and ranks of small squares in line with the requirements of Sudoku, I think of is: through the rows, columns and small square into a string, string elements required number n1, then the string into the set, then seek the number of elements in the collection, and then by comparing the values ​​n1 and n2 to know whether the Sudoku requirements for the number of dots [Note] processing
  • Row directly by traversing the list of contents can be determined given
  • List judged necessary to use two for loops to traverse
  • Analyzing small square on the more bother, the author uses the logic is: if a multiple of 3 by obtaining three rows, i.e., the first three of the list given; then the digital elements of each column of the row to different points required final judgment str1, str2, str3 while meeting several separate whether to; variables, such as 123 to the variable str1; 456 column by variable str2; 789 to the variable str3
  • If they are not compliant, you can return false exit

 

Script II: [when using: 100ms] [Reserved]

class Solution:
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        # init data
        rows = [{} for i in range(9)]
        columns = [{} for i in range(9)]
        boxes = [{} for i in range(9)]

        # validate a board
        for i in range(9):
            for j in range(9):
                num = board[i][j]
                if num != '.':
                    num = int(num)
                    box_index = (i // 3 ) * 3 + j // 3
                    
                    # keep the current cell value
                    rows[i][num] = rows[i].get(num, 0) + 1
                    columns[j][num] = columns[j].get(num, 0) + 1
                    boxes[box_index][num] = boxes[box_index].get(num, 0) + 1
                    
                    # check if this value has been already seen before
                    if rows[i][num] > 1 or columns[j][num] > 1 or boxes[box_index][num] > 1:
                        return False         
        return True

 


 

Script two logic:

  • The script did not understand the logic, it is to use some kind of visual correlation between the number of elements inside alone to solve. Interested friends can look in depth

 


 

shell Solution Share:

  • In the author's CSDN blog, before also used when learning the shell over shell to deal with this problem, the following is to use the shell to handle this problem link:
  • https://blog.csdn.net/weixin_43428906/article/details/102895947

Guess you like

Origin www.cnblogs.com/mailong/p/12070763.html