Determine if a 9 x 9 Sudoku board is valid.
Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
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.
这道题要求判断一个 9 x 9 的数独棋盘是否合法,核心是检查已经填入的数字是否同时满足三类约束:每一行不能有重复数字、每一列不能有重复数字、每个 3 x 3 小宫格也不能重复。题目并不要求数独一定可解,只需要验证当前已填部分是否违反规则即可。实现时通常用哈希集合或布尔标记分别记录行、列和宫格中出现过的数字,遍历整个棋盘时一旦发现重复就直接返回 false,整体思路简单,适合用一次遍历完成。
正文完