Daily LeetCode 36. Valid Sudoku
https://leetcode.com/problems/valid-sudoku/
Medium
问题描述
Determine if a 9x9 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 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
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
.
思路及代码:
这一题是今天早上算法课的测试中做到的题目
数独有三条规则:
- 每一行都由1~9组成,互不重复
- 每一列都有1~9组成,互不重复
- 每一个小的九宫格,也都由1~9组成,互不重复
我们只需要按照这三条规则,对给定的数独进行判断,看该数独是不是符合规则。
我们通过set()
方法来判断是否存在重复元素
1 | class Solution: |