【每日一题】36. Valid Sudoku

2020-08-12 15:49:03 浏览数 (2)

题目描述

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.

A partially filled sudoku which is valid.

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

Example 1:

代码语言:javascript复制
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:

代码语言:javascript复制
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.

判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

题解

判断数独矩阵是否有效,只要验证填入的数字即可,用符号’.'表示空格。面对这道题,首先想到的是循环 单次数字验证。写一个辅助函数,用于验证这个数字填在这是否有效:进行行验证、列验证以及3x3矩阵验证(i,j; i/3 3:小方阵的起始 i/3 * 3 3:终止位置);此外要验证的这个字符必须是数字才行;所以还需要进行字符的数字判断;最后进行99循环即可。

这种方法的缺点在于进行了多次重复验证;每个数字都要验证一次。为了节省时间,使用hash表对数字进行存储,边循环验证边存储(空间换时间);最后3x3小方阵对每个方阵进行一一验证。

具体实现,使用一个hash表unordered_map<int, int> cnt_r, unordered_map<int, int> cnt_c,分别表示验证的当前行出现的数字:

  • 如果当前字符board[i][j]出现在cnt_r, cnt_c中,说明发生重复,返回false;如果没有继续向下执行;
  • 如果当前字符是数字,将数字存储到hash表中;
  • 3x3方阵验证:每个方阵的横边界[i*3, i*3 3),竖边界为[j*3, j*3 3); 然后在这个3x3的矩阵进行有效性验证;使用hash表记录出现的数字
  • 如果执行到最后,返回true;

完整代码:

代码语言:javascript复制
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int i, j;
        
        for (i=0; i< 9; i  ){
            unordered_map<int, int> cnt_r;
            unordered_map<int, int> cnt_c;
            for (j=0; j< 9; j  ){
                if (cnt_r.find(board[i][j]) != cnt_r.end())
                    return false;
                if (board[i][j] != '.')
                    cnt_r[board[i][j]] = 1;
                if (cnt_c.find(board[j][i]) != cnt_c.end())
                    return false;
                if (board[j][i] != '.')
                    cnt_c[board[j][i]] = 1;
            }
        }
        
        for (i=0; i< 3; i  ) for(j=0; j< 3; j  ){
            unordered_map<int, int> cnt_part;
            for (int m=i*3; m< i*3 3; m  ) for (int n=j*3; n< j*3 3; n  ){
                if (cnt_part.find(board[m][n]) != cnt_part.end())
                    return false;
                if (board[m][n] != '.')
                    cnt_part[board[m][n]] = 1;
            }
        }
        return true;
    }
    
};

0 人点赞