LeetCode 36. Valid Sudoku 有效的数独(Java)

题目:

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-9 without 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.
    在这里插入图片描述
    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-9 and the character ‘.’.
  • The given board size is always 9x9.

解答:

解法一:暴力解法

自己只想到了采用暴力的解法,即通过三个循环,依次分别检查行、列和每个九宫格是否有效。并借助 hashmap 进行记录便于判断。

class Solution {
    public boolean isValidSudoku(char[][] board) {
        HashMap<Character, Integer> map = new HashMap<>();
        //判断每行
        for(int i=0; i<9; i++) {
            map.clear();
            for(int j=0; j<9; j++) {
                if(board[i][j] != '.' && map.containsKey(board[i][j])) {
                    return false;
                } else {
                    map.put(board[i][j], 1);
                }
            }
        }
        //判断每列
        for(int j=0; j<9; j++) {
            map.clear();
            for(int i=0; i<9; i++) {
                if(board[i][j] != '.' && map.containsKey(board[i][j])) {
                    return false;
                } else {
                    map.put(board[i][j], 1);
                }
            }
        }
        //判断每个九宫格
        int[][] flag = {{0,0}, {0,3}, {0,6}, {3,0}, {3,3}, {3,6}, {6,0}, {6,3}, {6,6} };
        for(int k=0; k<9; k++) {
            map.clear();
            for(int i=0; i<3; i++) {
                for(int j=0; j<3; j++) {
                    int row = flag[k][0] + i;
                    int column = flag[k][1] + j;
                    if(board[row][column] != '.' && map.containsKey(board[row][column])) {
                        return false;
                    } else {
                        map.put(board[row][column], 1);
                    }
                }
            }
        }
        return true;
    }
}

这种方法时间复杂度比较高。
在这里插入图片描述

解法二:优化后解法

后来看了他人的思路,对解法有一定的优化
这道题的三种情况判断中,其中最复杂的是第三种对九个九宫格的判断。我们对每个九宫格中的行列进行找规律,将其与行列序号关联起来。

9*9 数独可分为九个九宫格,我们从上至下,从左至右依次将九个九宫格标号为0~9
观察行号可得:

0️⃣	0	0	0			①	0	0	0			②	0	0	0
	1	1	1				1	1	1				1	1	1
	2	2	2				2	2	2				2	2	2

③	3	3	3			④	3	3	3			⑤	3	3	3
	4	4	4				4	4	4				4	4	4
	5	5	5				5	5	5				5	5	5

⑥	6	6	6			⑦	6	6	6			⑧	6	6	6
	7	7	7				7	7	7				7	7	7
	8	8	8				8	8	8				8	8	8


对于每三个九宫格行号增3。对于单个九宫格,每三个格点行号增1。
因此第i个九宫格的第j个格点的行号可表示为i/3*3+j/3(每个小九宫格j都是从0~9递增)

观察列号可得:

0️⃣	0	1	2			①	3	4	5			②	6	7	8
	0	1	2				3	4	5				6	7	8
	0	1	2				3	4	5				6	7	8

③	0	1	2			④	3	4	5			⑤	6	7	8
	0	1	2				3	4	5				6	7	8
	0	1	2				3	4	5				6	7	8

⑥	0	1	2			⑦	3	4	5			⑧	6	7	8
	0	1	2				3	4	5				6	7	8
	0	1	2				3	4	5				6	7	8


对于下个九宫格列号增3,循环周期为3。对于单个九宫格,每个格点列号增1,周期也为3。
因此第i个九宫格的第j个格点的列号可表示为i%3*3+j%3(每个小九宫格j都是从0~9递增)

因此,结合上述规律和 hashmap,我们可得到一下优化后的解法:

class Solution {
    public boolean isValidSudoku(char[][] board) {
        for(int i=0; i<9; i++) {
            HashSet<Character> row = new HashSet<>();
            HashSet<Character> column = new HashSet<>();
            HashSet<Character> sub = new HashSet<>();
            for(int j=0; j<9; j++) {
                //检查第i行
                if(board[i][j] !='.' && !row.add(board[i][j]))
                    return false;
                //检查第i列
                if(board[j][i] !='.' && !column.add(board[j][i]))
                    return false;
                //检查第i个九宫格
                int rowIndex = i/3*3 + j/3;
                int columnIndex = i%3*3 + j%3;
                if(board[rowIndex][columnIndex] !='.' && !sub.add(board[rowIndex][columnIndex]))
                    return false;
            }
        }
        return true;
    }
}

在这里插入图片描述
注意:HashSet 的 add() 方法
HashSet不能添加重复的元素,当调用 add(Object) 方法时,首先会调用 Object 的 hashCode 方法判 hashCode 是否已经存在,如不存在则直接插入元素。如果已存在则调用 Object 对象的 equals 方法判断返回值
add() 方法有返回值,如果传入的 Set 中存在的重复值,则表示 add 失败并返回 fasle,传入不存在的新值表示add成功,并返回true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值