LeetCode 36. Valid Sudoku (Medium)
题目
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
1.Each row must contain the digits 1-9 without repetition.
2.Each column must contain the digits 1-9 without repetition.
3.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.
Example 1:
Input: board =
[["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"]]
Output: true
Example 2:
Input: board =
[["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"]]
Output: false
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.
Constraints:
board.length == 9board[i].length == 9board[i][j]is a digit1-9or'.'.
思路
方法1 (Java)
设置一个Set集合,遍历数独表二维数组,将当前遍历到的位置的元素以3个字符串形式记录下它所在的行,列和九宫格信息,并加入Set集合。若Set集合中存在某一元素的任意行/列/九宫格信息,则认为该表不符合规则。
设当前元素值为val,位置为(i, j),位于第i/3*3+j/3个九宫格(从0开始),则该元素的信息可以设置为:
行信息:"val in row i"
列信息:"val in col j"
九宫格信息:"val in box i/3*3+j/3"
class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet<String> set = new HashSet<>();
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(board[i][j] == '.') continue;
String row = board[i][j] + " in row " + i;
String col = board[i][j] + " in col " + j;
String box = board[i][j] + " in box " + (i/3*3+j/3);
if(set.contains(row) || set.contains(col) || set.contains(box)) return false;
set.add(row);
set.add(col);
set.add(box);
}
}
return true;
}
}
方法2(Java)
从0-8,对每一个i,设置3个boolean数组分别检测第i行、第i列和第i个九宫格,数组的index代表被检测的值。
对于每个i的值,j从0-8意味着检测:
第i行, 第j个数是否已位于行i中;
第i列,第j个数是否已位于列i中;
第i个九宫格中,该九宫格从左上角到右下角的第j数是否已位于该九宫格中。
设置3个数组,
boolean[] checkRow 检查位置(i, j)的值是否已存在第i行
boolean[] checkCol 检查位置(j, i)的值是否已存在第i列
boolean[] checkBox 检查位置(i/3*3+j/3, j%3*3+j%3)的值是否已存在第i个九宫格中
例如当i为4,j从0-8,对应行、列、九宫格的遍历过程
0 1 2 3 4 5 6 7 8
0 o o o | o 1 o | o o o
1 o o o | o 2 o | o o o
2 o o o | o 3 o | o o o
------------------------
3 o o o | o 4 o | o o o
4 1 2 3 | 4 5 6 | 7 8 9
5 o o o | o 6 o | o o o
------------------------
6 o o o | o 7 o | o o o
7 o o o | o 8 o | o o o
8 o o o | o 9 o | o o o
第4个九宫格中
1 2 3
4 5 6
7 8 9
class Solution {
public boolean isValidSudoku(char[][] board) {
for(int i = 0; i < 9; i++){
boolean[] checkRow = new boolean[9];
boolean[] checkCol = new boolean[9];
boolean[] checkBox = new boolean[9];
for(int j = 0; j < 9; j++){
if(board[i][j] == '.') {}
else if(checkRow[board[i][j]-'1']) return false;
else checkRow[board[i][j]-'1'] = true;
if(board[j][i] == '.'){}
else if(checkCol[board[j][i]-'1']) return false;
else checkCol[board[j][i]-'1'] = true;
int m = i/3*3+j/3, n = i%3*3+j%3;
if(board[m][n] == '.'){}
else if(checkBox[board[m][n]-'1']) return false;
else checkBox[board[m][n]-'1'] = true;
}
}
return true;
}
}
LeetCode 36. Valid Sudoku (Medium)的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- [leetcode]36. Valid Sudoku验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- LeetCode 36. Valid Sudoku (C++)
题目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according t ...
- LeetCode 36 Valid Sudoku(合法的数独)
题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入 ...
随机推荐
- 第十五章---JSON
目录: (一)介绍 (二)Python 编码为 JSON 类型转换对应表 (三)JSON 解码为 Python 类型转换对应表 (四)实例 正文: (一)介绍 JSON (JavaScript Obj ...
- [hdu7044]Fall with Fake Problem
二分$T$和$S$第一个不同的位置,即需要对于$s$,判定是否存在$T[1,s]=S[1,s]$且满足条件的$T$ (注:这里的 ...
- [nowcoder5668H]Sort the Strings Revision
考虑对于$p_{i}=0$,那么可以快速比较出$s_{0},s_{1},...,s_{i-1}$与$s_{i},s_{i+1},...,s_{n}$之间的大小关系,然后对两边分别找到最小的$p_{i} ...
- 消息抽象层设计和实现-OSS.DataFlow
前面已经介绍了消息生产消费中间类库(OSS.DataFlow)的简单使用,这篇主要介绍内部的设计实现.主要内容包含: 1. 消息生产消费的抽象设计. 2. 具体使用示例 一. 消息生产消费的抽象设计. ...
- 如何在 ShardingSphere 中开发自己的 DistSQL
在<DistSQL:像数据库一样使用 Apache ShardingSphere>和<SCTL 涅槃重生:投入 RAL 的怀抱>中,已经为大家介绍了 DistSQL 的设计初衷 ...
- Codeforces 512E - Fox And Polygon(构造)
Codeforces 题面传送门 & 洛谷题面传送门 中规中矩的构造题一道. 首先考虑将两张图都向一个中间状态转化.方便起见我们取所有点都连向 \(1\) 号点的情形作为中间状态. 考虑怎样从 ...
- Python3编译安装ssl模块问题
本文以Centos系统为例 1.确保linux系统中安装了ssl-devel包 2.编译安装ssl模块到Python3中 1.查看linux系统中是否安装了ssl-devel包 # 查看命令 rpm ...
- 8核cpu,,负载
今天有一个电话面试,面试官问我:CentOS怎么查看CPU负载?我说:看top的第一行有load average.面试官又问:为什么从这就判定是负载高呢?依据是什么呢?然后... 然后我就尴尬了,挂了 ...
- 利用elliipse做相关图
参考资料:<数据探掘 R语言实战> p65-P68 install.packages("rattle") # 获取实验数据集 install.packages(&quo ...
- 使用SpringBoot实现文件的上传
使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...