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 == 9
board[i].length == 9
board[i][j]
is a digit1-9
or'.'
.
思路
方法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 其中,未填入 ...
随机推荐
- Alpine容器安装运行ssh
写在前面 本文介绍了在Alpine容器(docker)上安装运行ssh并保证外界(宿主机)能通过ssh登录的方法,给出了相应的命令.在下在探索过程中借鉴了许多前人的经验,在此先行谢过,所有参考内容都会 ...
- [loj6254]最优卡组
特殊处理$c_{i}=1$的$i$,显然对这些$a_{i,1}$求和即可,以下都假设$c_{i}\ge 2$ 对于每一个$i$,将$a_{i,j}$从大到小排序:接下来,对于所有$i$,按照$a_{i ...
- [bzoj1927]星际竞速
考虑没有爆发,那么相当于是带权最小不可交路径覆盖,由于只能从编号小的到编号大的,因此一定是DAG,而DAG的最小路径覆盖可以拆点并跑最大流,那么带权的只需要跑费用流即可(S向i连(1,0)的边,i'向 ...
- 国内首家!腾讯云正式成为 FinOps 基金会顶级会员
11月24日,腾讯云正式宣布加入FinOps基金会,作为国内首家FinOps基金会顶级会员,腾讯云将联合FinOps基金会,全面推进对FinOps标准和最佳实践的贡献,为企业提供云财务管理的最佳解决方 ...
- 31、下一个排列 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(31)下一个排列 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "双指针法 ...
- CentOS编译openjdk
编译openjdk 1. 下载openjdk源码 openjdk的官网是OpenJDK (java.net) 在网站左侧就能看到它的源码位置的链接 从图上可以看到,它的源码在两个位置有托管,Mercu ...
- Identity Server 4 从入门到落地(一)—— 从IdentityServer4.Admin开始
最近项目中需要使用Identity Server 4,以前对这个技术只是有些了解,没有系统研究过,网上相关的资料不少,大多是从编写一个简单的认证服务开始,离能够落地使用有相当的距离,理论学习如何不结合 ...
- 洛谷 P4437 [HNOI/AHOI2018]排列(贪心+堆,思维题)
题面传送门 开始 WA ycx 的遗产(bushi 首先可以将题目转化为图论模型:\(\forall i\) 连边 \(a_i\to i\),然后求图的一个拓扑序 \(b_1,b_2,\dots b_ ...
- 【豆科基因组】大豆(Soybean, Glycine max)泛基因组2020Cell
目录 一.前沿概述 二.主要结果 重测序.组装与注释 泛基因组 SV特征 PAV与古多倍化,WGD事件 基因SV与基因融合 SV与大豆驯化 SV影响基因表达及其与性状关联 一.前沿概述 Pan-Gen ...
- Django向数据库批量插入数据
# 如何向数据库一次性插入多条数据 # 方法一:效率极低,不推荐使用 for i in range(1000): models.Book.objects.create(title=f'第{i}本书') ...