美文网首页
leetcode 初级之数组篇 10

leetcode 初级之数组篇 10

作者: ngugg | 来源:发表于2018-09-19 07:31 被阅读0次

36.有效的数独

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

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

<small style="box-sizing: border-box; font-size: 11.899999618530273px;">上图是一个部分填充的有效的数独。</small>

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

示例 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 存在, 因此这个数独是无效的。

12ms 战胜 94.62% 的cpp 提交

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
       if (board.empty() || board[0].empty()) return false;
        int m = board.size(), n = board[0].size();
        vector<vector<bool> > rowFlag(m, vector<bool>(n, false));
        vector<vector<bool> > colFlag(m, vector<bool>(n, false));
        vector<vector<bool> > cellFlag(m, vector<bool>(n, false));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (board[i][j] >= '1' && board[i][j] <= '9') {
                    int c = board[i][j] - '1';
                    if (rowFlag[i][c] || colFlag[c][j] || cellFlag[3 * (i / 3) + j / 3][c]) return false;
                    rowFlag[i][c] = true;
                    colFlag[c][j] = true;
                    cellFlag[3 * (i / 3) + j / 3][c] = true;
                }
            }
        }
        return true;
    }
};

参考文章:
https://blog.csdn.net/biezhihua/article/details/79648015

http://www.cnblogs.com/grandyang/p/4421217.html

相关文章

  • leetcode 初级之数组篇 10

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

  • leetcode 初级之数组篇 01

    26. 删除排序数组中的重复项 两种方法的比较: 第一种方法,是前后两个元素是否相等,如果不等,将其存储到k所指示...

  • leetcode 初级之数组篇 04

    存在重复 给定一个整数数组,判断是否存在重复元素。 如果任何值在数组中出现至少两次,函数返回 true。如果数组中...

  • leetcode 初级之数组篇 05

    只出现一次的数字 我们可以考虑 异或运算 ,它是满足交换律和结合的,也就是说 abc = acb,这样当我们遍历数...

  • leetcode 初级之数组篇 06

    两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集。 示例 1: 输入: nums1 = [1,2,...

  • leetcode 初级之数组篇 03

    旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。示例 1:输入: [1,2,3,4...

  • leetcode 初级之数组篇 02

    买卖股票的最佳时机 II 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。设计一个算法来计算你所能...

  • leetcode 初级之数组篇 08

    283. Move Zeroes 移动零给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持...

  • leetcode 初级之数组篇 07

    加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个...

  • leetcode 初级之数组篇 09

    两数之和 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的...

网友评论

      本文标题:leetcode 初级之数组篇 10

      本文链接:https://www.haomeiwen.com/subject/nrdbnftx.html