题目

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 digit 1-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的值,j0-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)的更多相关文章

  1. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  2. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  4. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  5. leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  6. [LeetCode] 36. Valid Sudoku 验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  7. [leetcode]36. Valid Sudoku验证数独

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

  8. LeetCode 36. Valid Sudoku (C++)

    题目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according t ...

  9. LeetCode 36 Valid Sudoku(合法的数独)

    题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description   给出一个二维数组,数组大小为数独的大小,即9*9  其中,未填入 ...

随机推荐

  1. 修改eclipse中注释字体而不影响代码字体

    eclipse的注释字体大小如何修改?不改变代码的字体 貌似没有直接的办法,但是可以取个巧: Window --> Preferences --> General --> Appea ...

  2. [bzoj1146]网络管理

    发现是链上的问题,所以树链剖分发现要查询第k大,因为第k大不支持合并,所以要二分答案二分答案后相当于询问一些区间内大于某数的数个数,直接线段树套平衡树即可时间复杂度$o(nlog^{4}_n)$(跟$ ...

  3. [loj3343]超现实树

    定义1:两棵树中的$x$和$y$对应当且仅当$x$到根的链与$y$到根的链同构 定义2:$x$和$y$的儿子状态相同当且仅当$x$与儿子所构成的树与$y$与儿子所构成的树同构 根据题中所给的定义,有以 ...

  4. [loj3302]信号传递

    由于n较大,可以将n个数中的关系对数量记录在$m*m$的矩阵中,记作$a[i][j]$ 考虑朴素的状压dp枚举排列,即$f[i]$表示以i中的数的一种排列为整个序列的前缀的最小代价,然后转移枚举下一个 ...

  5. SpringServletContainerInitializer的代码流程

    SpringServletContainerInitializer 是spring中的一个class实现了servlet3.0规范的一个接口 implements ServletContainerIn ...

  6. 互联网java面试宝典

    1.为什么使用消息队列啊? 答题: 消息队列的核心功能就是:解耦合,异步,流量削峰解耦:接口调用发送,那如果E系统也要这个数据呢?那如果C系统现在不需要了呢?现在A系统又要发送第二种数据了呢?A系统负 ...

  7. Rainbond通过插件整合SkyWalking,实现APM即插即用

    作者:张震 一. 简介 SkyWalking 是一个开源可观察性平台,用于收集.分析.聚合和可视化来自服务和云原生基础设施的数据.支持分布式追踪.性能指标分析.应用和服务依赖分析等:它是一种现代 AP ...

  8. c语言实参与形参的区别

    1 #include<stdio.h> 2 #include<math.h> 3 4 /** 5 * 形参和实参的功能是作数据传送. 6 * 函数调用中发生的数据传送是单向的. ...

  9. CF1550D Excellent Arrays

    考虑每个数一定是这个形式: \(i + x,i - x\) 所以如果我们要构造一个最大的数组. 那一定是这样的形式: 有一半为\(i + x\),有一半\(i - x\) 那么我们发现每个数有这样一个 ...

  10. 洛谷 P6624 - [省选联考 2020 A 卷] 作业题(矩阵树定理+简单数论)

    题面传送门 u1s1 这种题目还是相当套路的罢 首先看到 \(\gcd\) 可以套路地往数论方向想,我们记 \(f_i\) 为满足边权的 \(\gcd\) 为 \(i\) 的倍数的所有生成树的权值之和 ...