代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。