13.有效的数独

36. 有效的数独

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

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

 

注意:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。
  • 空白格用 '.' 表示。

 

示例 1:

 

输入:board = 
[["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:

输入:board = 
[["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 存在, 因此这个数独是无效的。

 

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字(1-9)或者 '.'

自己的暴力解法(未解决3×3块内问题):

 1 class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         //遍历所有数字
 4         for (int i = 0;i < 9;i++){
 5             for(int j = 0;j < 9;j++){
 6                 int temp = board[i][j];
 7                 //判断行是否有重复的数字
 8                 for(int a = 0;a < 9;a++){
 9                     if(a == j){
10                         continue;
11                     }else if(board[i][a] == board[i][j]){
12                         return false;
13                     }
14                 }
15                 //判断列是否有重复的数字
16                 for(int b = 0;b < 9;b++){
17                     if(b == i){
18                         continue;
19                     }else if(board[b][j] == board[i][j]){
20                         return false;
21                     }
22                 }
23                 //判断3×3宫内是否有重复数字
24                 
25             }
26         }
27         return true;
28     }
29 }

查看题解:

下面这个图是每一个3 * 3格子所在的blockIndex

0 1 2

3 4 5

6 7 8

先看列,是按照加一的方式增加,所以是j / 3

再看行,是按照0*3 1*3 2*3 的方式增加 所以先除3算出前面的数,也就是0 1 2 ,再乘3

行 + 列 就是方格中元素的blockIndex

 1 class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         // 记录某行,某位数字是否已经被摆放
 4         boolean[][] row = new boolean[9][9];
 5         // 记录某列,某位数字是否已经被摆放
 6         boolean[][] col = new boolean[9][9];
 7         // 记录某 3x3 宫格内,某位数字是否已经被摆放
 8         boolean[][] block = new boolean[9][9];
 9 
10         for (int i = 0; i < 9; i++) {
11             for (int j = 0; j < 9; j++) {
12                 if (board[i][j] != '.') {
              // 将字符转化为int型的整数,方便后续使用其下标。-‘1’的妙用;
13 int num = board[i][j] - '1';
              // 判断数字在哪一块中,也可是i / 3 + j / 3 * 3;
14 int blockIndex = i / 3 * 3 + j / 3;
              // 置0(默认)为对,若有非0即满足判断条件(该位置出现了1-9只能在行、列、块中只出现一次的问题)并判错
15 if (row[i][num] || col[j][num] || block[blockIndex][num]) { 16 return false; 17 } else { 18 row[i][num] = true; 19 col[j][num] = true; 20 block[blockIndex][num] = true; 21 } 22 } 23 } 24 } 25 return true; 26 } 27 }

 

posted on 2022-06-17 01:23  HHHuskie  阅读(65)  评论(0编辑  收藏  举报

导航