当前位置:网站首页>【LeetCode-36】有效的数独
【LeetCode-36】有效的数独
2022-08-11 05:30:00 【Ring*】
7.2 有效的数独【36】
7.2.1 题目描述
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字 1-9 在每一行只能出现一次。
- 数字 1-9 在每一列只能出现一次。
- 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 空白格用 ‘.’ 表示。
示例 1:

7.2.2 方法一:一次遍历

class Solution {
public boolean isValidSudoku(char[][] board) {
int[][] rows = new int[9][9]; // rows[i][j]记录第i行j+1这个数出现的次数
int[][] columns = new int[9][9]; // rows[i][j]记录第j列i+1这个数出现的次数
int[][][] subboxes = new int[3][3][9]; // 记录每一个小矩阵出现1-9的次数
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c != '.') {
int index = c - '0' - 1;
rows[i][index]++;
columns[j][index]++;
subboxes[i / 3][j / 3][index]++;
if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
return false;
}
}
}
}
return true;
}
}
复杂度分析
- 时间复杂度:O(1)。数独共有 81 个单元格,只需要对每个单元格遍历一次即可。
- 空间复杂度:O(1)。由于数独的大小固定,因此哈希表的空间也是固定的。
7.2.3 my answer—哈希表
class Solution {
public boolean isValidSudoku(char[][] board) {
if(!isValid3x3(board))return false; // 判断每一个3×3的小矩阵1-9是否只出现一次
// 判断每一行
for (int i = 0; i < board.length; i++) {
char temp[] = new char[board[i].length];
temp = Arrays.copyOf(board[i],board[i].length);
if(!isValid1x9(temp)){
return false;
}
}
// 判断每一列
for (int j = 0; j < board[0].length; j++) {
char temp[] = new char[board.length];
for (int i = 0; i < board.length; i++) {
temp[i] = board[i][j];
}
if(!isValid1x9(temp)){
return false;
}
}
return true;
}
// 判断1×9的数组中1-9是否只出现一次
public static boolean isValid1x9(char[] board){
Map<Character,Integer> map = new HashMap<>();
for (int i = 0; i < board.length; i++) {
if(board[i] != '.'){
map.put((Character)board[i],map.getOrDefault((Character)board[i],0)+1);
}
}
for (Map.Entry<Character, Integer> characterIntegerEntry : map.entrySet()) {
if(characterIntegerEntry.getValue()>1){
return false;
}
}
return true;
}
// 判断每一个3×3的小矩阵1-9是否只出现一次
public static boolean isValid3x3(char[][] board){
for(int i=0;i<3;i++){
for(int j = 0;j<3;j++){
// int[][] temp = new int[3][3];
Map<Character,Integer> map = new HashMap<>();
for(int m = i*3;m<(i+1)*3;m++){
for(int n = j*3;n<(j+1)*3;n++){
// temp[m%3][n%3] = board[m][n];
if(board[m][n] != '.'){
map.put((Character)board[m][n],map.getOrDefault((Character)board[m][n],0)+1);
}
}
}
for (Map.Entry<Character, Integer> characterIntegerEntry : map.entrySet()) {
if(characterIntegerEntry.getValue()>1){
return false;
}
}
}
}
return true;
}
}
边栏推荐
- C语言-7月19日-指针的学习
- gerrit configure SSH Key and account, email information
- OpenMLDB官网升级,神秘贡献者地图带你快速进阶
- Regular expression replacement for batch quick modification code
- JS事件循环机制
- 解决npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
- jdbc接口文档参考,jdbc接口方法逻辑探究
- C-8月1日-递归与动态内存管理
- 使用adb命令管理应用
- 智能风控中台设计与落地
猜你喜欢
![swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null](/img/46/6ceeb330821d28b44742436da4123c.png)
swagger错误:WARN i.s.m.p.AbstractSerializableParameter - [getExample,421] - Illegal DefaultValue null

构建面向特征工程的数据生态 ——拥抱开源生态,OpenMLDB全面打通MLOps生态工具链

Day 81

The third phase of the contributor task is wonderful

自己动手写RISC-V的C编译器-01实现加减法

OpenMLDB:线上线下一致的生产级特征计算平台

JS advanced web page special effects (pink teacher notes)

2022DASCTF X SU 三月春季挑战赛 checkin ROPgadget进阶使用

第六届蓝帽杯 EscapeShellcode

经纬度求距离
随机推荐
The whole process of Tinker access --- Compilation
Visual studio2019 配置使用pthread
Day 73
Jetpack use exception problem collection
ARM assembly instruction ADR and LDR
星盟-pwn-babyheap
深度学习Matlab工具箱代码注释
C语言-7月21日-指针的深入
Day 67
微信小程序启动页的实现
Js method commonly used objects and attributes
JS case exercise (classic case of teacher pink)
将一个excel文件中多个sheet页“拆分“成多个“独立“excel文件
OpenMLDB官网升级,神秘贡献者地图带你快速进阶
swagger常用注释API @ApiModel、@ApiModelProperty的用法
mongoose连接mongodb不错,显示encoding没有定义
He Kaiming's new work ViTDET: target detection field, subverting the concept of layered backbone
PyQt5中调用.ui转换的.py文件代码解释
【无标题】
父子节点数据格式不一致的树状列表实现