2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_36.java 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-02-23 17:07 . 新增简单算法题
package com.hit.basmath.learn.hash_table;
import java.util.HashSet;
import java.util.Set;
/**
* 36. Valid Sudoku
* <p>
* Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
* <p>
* 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 digits 1-9 without repetition.
* <p>
* A partially filled sudoku which is valid.
* <p>
* The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
* <p>
* Example 1:
* <p>
* Input:
* <p>
* [
* ["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"]
* ]
* <p>
* Output: true
* <p>
* Example 2:
* <p>
* Input:
* <p>
* [
* ["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"]
* ]
* <p>
* Output: false
* <p>
* 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.
* <p>
* Note:
* <p>
* 1. A Sudoku board (partially filled) could be valid but is not necessarily solvable.
* 2. Only the filled cells need to be validated according to the mentioned rules.
* 3. The given board contain only digits 1-9 and the character '.'.
* 4. The given board size is always 9x9.
*/
public class _36 {
public boolean isValidSudoku(char[][] board) {
Set<String> seen = new HashSet<>();
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
char number = board[i][j];
if (number != '.') {
if (!seen.add(number + " in row " + i) ||
!seen.add(number + " in column " + j) ||
!seen.add(number + " in block " + i / 3 + "-" + j / 3)) {
return false;
}
}
}
}
return true;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助