• LeetCode(36): 有效的数独


    Medium!

    题目描述:

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

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

    上图是一个部分填充的有效的数独。

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    示例 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:

    输入:
    [
      ["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"]
    ]
    输出: false
    解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
         但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

    说明:

    • 一个有效的数独(部分已被填充)不一定是可解的。
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • 给定数独序列只包含数字 1-9 和字符 '.' 。
    • 给定数独永远是 9x9 形式的。

    解题思路:

    这道题让我们验证一个方阵是否为数独矩阵。判断标准是看各行各列是否有重复数字,以及每个小的3x3的小方阵里面是否有重复数字,如果都无重复,则当前矩阵是数独矩阵,但不代表该数独矩阵有解,只是单纯的判断当前未填完的矩阵是否是数独矩阵。

    根据数独矩阵的定义,在遍历每个数字的时候,就看看包含当前位置的行和列以及3x3小方阵中是否已经出现该数字,那么我们需要三个标志矩阵,分别记录各行,各列,各小方阵是否出现某个数字,其中行和列标志下标很好对应,就是小方阵的下标需要稍稍转换一下。

    C++解法一:

     1 class Solution {
     2 public:
     3     bool isValidSudoku(vector<vector<char> > &board) {
     4         if (board.empty() || board[0].empty()) return false;
     5         int m = board.size(), n = board[0].size();
     6         vector<vector<bool> > rowFlag(m, vector<bool>(n, false));
     7         vector<vector<bool> > colFlag(m, vector<bool>(n, false));
     8         vector<vector<bool> > cellFlag(m, vector<bool>(n, false));
     9         for (int i = 0; i < m; ++i) {
    10             for (int j = 0; j < n; ++j) {
    11                 if (board[i][j] >= '1' && board[i][j] <= '9') {
    12                     int c = board[i][j] - '1';
    13                     if (rowFlag[i][c] || colFlag[c][j] || cellFlag[3 * (i / 3) + j / 3][c]) return false;
    14                     rowFlag[i][c] = true;
    15                     colFlag[c][j] = true;
    16                     cellFlag[3 * (i / 3) + j / 3][c] = true;
    17                 }
    18             }
    19         }
    20         return true;
    21     }
    22 };
  • 相关阅读:
    现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
    mRNA(阅读链接)
    技术的极限(13): 对过程和细节的可视化
    现代编程语言:TypeScript
    现代编程语言:JavaScript
    现代编程语言:zig
    现代编程语言:Python(蛇形遍历一颗树)
    炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
    软件性能测试分析与调优实践之路-Web中间件的性能分析与调优总结
    退役记
  • 原文地址:https://www.cnblogs.com/ariel-dreamland/p/9138330.html
Copyright © 2020-2023  润新知