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-9without 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:

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-9and the character '.'.
  • The given board size is always 9x9.

这道题让验证一个方阵是否为数独矩阵,想必数独游戏我们都玩过,就是给一个 9x9 大小的矩阵,可以分为9个 3x3 大小的矩阵,要求是每个小矩阵内必须都是1到9的数字不能有重复,同时大矩阵的横纵列也不能有重复数字,是一种很经典的益智类游戏,经常在飞机上看见有人拿着纸笔在填数,感觉是消磨时光的利器。这道题给了一个残缺的二维数组,让我们判断当前的这个数独数组是否合法,即要满足上述的条件。判断标准是看各行各列是否有重复数字,以及每个小的 3x3 的小方阵里面是否有重复数字,如果都无重复,则当前矩阵是数独矩阵,但不代表待数独矩阵有解,只是单纯的判断当前未填完的矩阵是否是数独矩阵。那么根据数独矩阵的定义,在遍历每个数字的时候,就看看包含当前位置的行和列以及 3x3 小方阵中是否已经出现该数字,这里需要三个 boolean 型矩阵,大小跟原数组相同,分别记录各行,各列,各小方阵是否出现某个数字,其中行和列标志下标很好对应,就是小方阵的下标需要稍稍转换一下,具体代码如下:

解法一:

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

我们也可以对空间进行些优化,只使用一个 HashSet 来记录已经存在过的状态,将每个状态编码成为一个字符串,能将如此大量信息的状态编码成一个单一的字符串还是需要有些技巧的。对于每个1到9内的数字来说,其在每行每列和每个小区间内都是唯一的,将数字放在一个括号中,每行上的数字就将行号放在括号左边,每列上的数字就将列数放在括号右边,每个小区间内的数字就将在小区间内的行列数分别放在括号的左右两边,这样每个数字的状态都是独一无二的存在,就可以在 HashSet 中愉快地查找是否有重复存在啦,参见代码如下:

解法二:

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
unordered_set<string> st;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
if (board[i][j] == '.') continue;
string t = "(" + to_string(board[i][j]) + ")";
string row = to_string(i) + t, col = t + to_string(j), cell = to_string(i / ) + t + to_string(j / );
if (st.count(row) || st.count(col) || st.count(cell)) return false;
st.insert(row);
st.insert(col);
st.insert(cell);
}
}
return true;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/36

类似题目:

Sudoku Solver

参考资料:

https://leetcode.com/problems/valid-sudoku/

https://leetcode.com/problems/valid-sudoku/discuss/15450/Shared-my-concise-Java-code

https://leetcode.com/problems/valid-sudoku/discuss/15472/Short%2BSimple-Java-using-Strings

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 36. Valid Sudoku 验证数独的更多相关文章

  1. [leetcode]36. Valid Sudoku验证数独

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

  2. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  3. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  4. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  5. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  7. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. LeetCode 36 Valid Sudoku(合法的数独)

    题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description   给出一个二维数组,数组大小为数独的大小,即9*9  其中,未填入 ...

  9. LeetCode 36. Valid Sudoku (Medium)

    题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...

随机推荐

  1. java的递归异常—一个异常可能由另一个异常触发

    关键字: Caused by nested exception java.lang.reflect.InvocationTargetException: null at sun.reflect.Nat ...

  2. sap和OA之间数值传递2(工程创建)

    1.创建project. 右击--new-other

  3. gitea/gogs在push操作时报RPC failed的问题

    原文发布于:https://www.chenxublog.com/2019/05/26/gitea-gogs-push-rpc-failed.html 最近川普在搞出口管制,GitHub也更新了相应的 ...

  4. //某父元素(.class)底下相同class的第二的取值

    //某父元素(.class)底下相同class的第二的取值 var v = $('.cell-right').find(".startime").eq(1).val();

  5. 实战Rest API接口测试

    一.Android App API接口测试 1.如何学好Android App API接口测试 postman可以用来实现API接口自动化测试,但是也有弊端,无法实现接口测试数据的参数化,为了达到接口 ...

  6. Linux管道及重定向

    Linux管道及重定向 对shell有一定了解的人都知道,管道和重定向是 Linux 中非常实用的 IPC 机制.在shell中,我们通常使用符合'|'来表示管道,符号'>'和'<'表示重 ...

  7. 腾讯WeTest加入智慧零售“倍增计划”,引领微信小程序质量优化

    WeTest 导读 在2019腾讯全球数字生态大会零售分论坛上,腾讯正式面向全行业合作伙伴发布倍增计划,通过咨询.培训.竞赛三步走,帮助零售商户解决前端触点融通的问题,推动微信生意大盘阶梯式上涨. 倍 ...

  8. Windows中将nginx添加到服务(转)

    下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...

  9. 微信小程序滚动tab的实现

    微信小程序滚动tab的实现 1.目的:为了解决滚动版本的tab,以及实现虹吸效果. 2.方案:自己动手写了一个Demo,用于测试实现如上的效果.其代码有做参考,在这里先声明.具体的参照如下:重庆大学二 ...

  10. Spring框架完全掌握(上)

    引言 前面我写了一篇关于Spring的快速入门,旨在帮助大家能够快速地了解和使用Spring.既然是快速入门,讲解的肯定只是一些比较泛的知识,那么对于Spring的一些深入内容,我决定将其分为上.下两 ...