[HashTable]036 Valid Sudoku***

  • 分类:HashTable

  • 考察知识点:HashTable 数组遍历

  • 最优解时间复杂度:O(n^2)

36. Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
[HashTable]036 Valid Sudoku***_第1张图片
A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

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.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

代码:

HashMap解法O(n^2):

class Solution:
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in range(9):
            #初始化三个dict
            row_dict={}
            col_dict={}
            cube_dict={}
            for j in range(9):
                #判断横行
                if board[i][j]!=".":
                    if board[i][j] in row_dict:
                        return False
                    else:
                        row_dict[board[i][j]]=1
                    
                #判断纵行
                if board[j][i]!=".":
                    if board[j][i] in col_dict:
                        return False
                    else:
                        col_dict[board[j][i]]=1
                    
                #判断Cube
                rowIndex=(i//3)*3
                colIndex=(i%3)*3
                if board[rowIndex+j//3][colIndex+j%3]!=".":
                    if board[rowIndex+j//3][colIndex+j%3] in cube_dict:
                        return False
                    else:
                        cube_dict[board[rowIndex+j//3][colIndex+j%3]]=1
                
        return True

普通解法O(n^3):

class Solution:
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in range(9):
            for j in range(9):
                if board[i][j]!=".":
                    if (self.isValid(board,i,j))==False:
                        return False
                    
        return True
    
    def isValid(self,board,row,col):
        #检验横行和纵行
        for i in range(9):
            if row!=i:
                if board[i][col]==board[row][col]:
                    return False
            if col!=i:
                if board[row][i]==board[row][col]:
                    return False
        #检验cube
        for i in range(row//3*3,(row//3+1)*3):
            for j in range(col//3*3,(col//3+1)*3):
                if i!=row and j!=col:
                    if board[i][j]==board[row][col]:
                        return False
                    
        return True

讨论:

1.这道题目非常重要,在公司的面试中出现的概率特别的高
2.有两种写法,一种是用HashMap,一种就是普通判断
3.对于这个i//3和i%3的方法可以熟练一下,比较取巧,但是还是可以用的!

[HashTable]036 Valid Sudoku***_第2张图片
一般解速度
[HashTable]036 Valid Sudoku***_第3张图片
最优解速度

你可能感兴趣的:([HashTable]036 Valid Sudoku***)