当前位置:网站首页>【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;
}
}
边栏推荐
- 无效的修订:3.18.1-g262b901-dirty
- The third phase of the contributor task is wonderful
- 【无标题】
- 127.0.0.1 connection refused
- Wonderful linkage | OpenMLDB Pulsar Connector principle and practical operation
- Day 75
- Some formulas for system performance and concurrency
- Compilation exception resolution
- 基于微信小程序云开发实现的电商项目,可以自行定制开发
- C语言-7月21日-指针的深入
猜你喜欢
随机推荐
JS案例练习(pink老师经典案例)
自己动手写RISC-V的C编译器-02语法描述方法和递归下降解析
智能风控中台设计与落地
127.0.0.1 connection refused
swagger常用注释API @ApiModel、@ApiModelProperty的用法
OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
JS this关键字
Day 70
黑马大事件项目
The official website of OpenMLDB is upgraded, and the mysterious contributor map will take you to advance quickly
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
gerrit configure SSH Key and account, email information
Day 79
Scene-driven feature calculation method OpenMLDB, efficient implementation of "calculate first use"
轻松理解进程与线程
【剑指offer系列】面试题日记(前期题)
js learning advanced BOM part (pink teacher notes)
Day 68
Use the adb command to manage applications
OpenMLDB v0.5.0 released | Performance, cost, flexibility reach new heights









