36. Valid Sudoku
题目
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
题目大意
给定一个二维数组代表数独,判断是否满足数独的定义,数独需要满足的规则有每一行1-9不能重复,每一列1-9不能重复,每一个块(一共3*3=9块)1-9不能重复. 点代表未知的任意数,
代码
1 | class Solution { |
解答
使用哈希表,used1 表示判断每行,used2表示判断没列,used3表示判断每个块.
从上到下,从左到右,对每一个数进行遍历,i,j,k 分别代表当前遍历到的块走到了哪一个行,列,块.
有了就在对应的used标记为1,若有重复标记的1 .直接return false.
Author: corn1ng
Link: https://corn1ng.github.io/2018/01/02/算法/leetcode36/
License: 知识共享署名-非商业性使用 4.0 国际许可协议