首页 > 其他 > 详细

[LeetCode] 36. Valid Sudoku_Medium tag: Array

时间:2021-06-09 12:52:00      阅读:24      评论:0      收藏:0      [点我收藏+]

Determine if a 9 x 9 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 nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

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.

 

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: 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.

 

Constraints:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit or ‘.‘.

 

Idea: 用两个for loop 去检测每一个点,然后看该行,该列和所在的3 *3的小正方形中是否有相同的数,如果有表明invalid,最后两个for loop结束,返回True。

我们利用3个9 * 9的array去记录,

rowFlag[row][num] 的左下标row表示的是行数,右下标num表示的是(“实际数字” - 1),数值rowFlag[row][num]如果为False表示目前该row行没有出现过num + 1 这个数字,如果为True表明出现过num + 1这个数字。

同理,colFlag[col][num] 的左下标col表示的是列数,右下标num表示的是(“实际数字” - 1),数值colFlag[col][num]如果为False表示目前该col列没有出现过num + 1 这个数字,如果为True表明出现过num + 1这个数字。

最后,cellFlag[cellIndex][num]的左下标cellIndex表示的是哪个小正方形的index, 如下图所示,图是来自Leetcode这道题的solution,右下标num表示的是(“实际数字” - 1),数值cellFlag[cellIndex][num]如果为False表示目前该cellIndex的小正方形没有出现过num + 1 这个数字,如果为True表明出现过num + 1这个数字。

cellIndex = (row //3) * 3 + col //3          , 可以记住,或者自己推一下就可以了,可以带入row 和col进去试试看就明白了。

技术分享图片

 

 n = 9 in this case

T: O(n * n)

S: O(n * n)

 

Code

class Solution:
    def isValidSudoku(self, board: List[list[str]]) -> bool:
        rowFlag = [[False] * 9 for _ in range(9)]
        colFlag =  [[False] * 9 for _ in range(9)]
        cellFlag =  [[False] * 9 for _ in range(9)]
        for i in range(9):
            for j in range(9):
                if board[i][j] == .:
                    continue
                num = int(board[i][j]) - 1
                cellIndex = (i//3) * 3 + j //3
                if rowFlag[i][num] or colFlag[j][num] or cellFlag[cellIndex][num]:
                    return False
                rowFlag[i][num] = True
                colFlag[j][num] = True
                cellFlag[cellIndex][num] = True
        return True

 

[LeetCode] 36. Valid Sudoku_Medium tag: Array

原文:https://www.cnblogs.com/Johnsonxiong/p/14865592.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!