【每日一题】36. Valid Sudoku
题目描述
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-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.
判断一个 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;
完整代码:
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;
}
};
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法