Valid Sudoku leetcode java
题目:
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.
题解:
这道题利用的是HashSet的唯一性来帮助check。
先按每行check,如果是'.'说明还没填字,是合法的,往下走,如果没在set中存过就加一下,如果便利过程中出现了在set中存在的key值,说明有重复的数字在一行,不合法,return false。
再按照这个方法check列。
最后按照这个方法check小方块。
注意小方块的ij取法。对于当前这块板子来说,总共有9个小方格,按0~8从左到右依次编号。
按编号求'/'就是求得当前小方格的第一行横坐标,因为每个小方格有3行,所以循环3次。
按编号求'%'就是求得当前小方格的第一列纵坐标,因为每个小方格有3列,所以循环3次。
对9个小方格依次走一边,就完成了检查小方格的工作。
代码如下:
1 public boolean isValidSudoku(char[][] board) {
2 HashSet<Character> set = new HashSet<Character>();
3 // Check for each row
4 for (int i = 0; i < 9; i++) {
5 for (int j = 0; j < 9; j++) {
6 if (board[i][j] == '.')
7 continue;
8 if (set.contains(board[i][j]))
9 return false;
set.add(board[i][j]);
}
set.clear();
}
// Check for each column
for (int j = 0; j < 9; j++) {
for (int i = 0; i < 9; i++) {
if (board[i][j] == '.')
continue;
if (set.contains(board[i][j]))
return false;
set.add(board[i][j]);
}
set.clear();
}
// Check for each sub-grid
for (int k = 0; k < 9; k++) {
for (int i = k/3*3; i < k/3*3+3; i++) {
for (int j = (k%3)*3; j < (k%3)*3+3; j++) {
if (board[i][j] == '.')
continue;
if (set.contains(board[i][j]))
return false;
set.add(board[i][j]);
}
}
set.clear();
}
return true;
}
Valid Sudoku leetcode java的更多相关文章
- Valid Sudoku leetcode
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- Valid Palindrome leetcode java
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- LeetCode——Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- leetcode@ [36/37] Valid Sudoku / Sudoku Solver
https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puz ...
随机推荐
- 闭包应用之延迟函数setTimeout
根据HTML 5标准,setTimeout推迟执行的时间,最少是5毫秒.如果小于这个值,会被自动增加到5ms. 每一个setTimeout在执行时,会返回一个唯一ID,把该ID保存在一个变量中,并传入 ...
- P4810 A’s problem(a)
P4810 A’s problem(a)From: admin 时间: 1000ms / 空间: 65536KiB / Java类名: Main 背景 清北NOIP春季系列课程 描述 这是一道有背景的 ...
- hdu 4461 第37届ACM/ICPC杭州赛区I题
题意:给两个人一些棋子,每个棋子有其对应的power,若b没有或者c没有,或者二者都没有,那么他的total power就会减1,total power最少是1,求最后谁能赢 如果b或c出现的话,fl ...
- 15、Redis的集群
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------------------- ...
- Windows和linux下clock函数
windows: Calculates the wall-clock time used by the calling process. return:The elapsed wall-clock ...
- STM32 通用定时器的几种配置方式
STM32 通用定时器的几种配置方式 //------------------------------------------------------------------------------ ...
- Timer-triggered memory-to-memory DMA transfer demonstrator
http://www.efton.sk/STM32/bt.c // Timer-triggered memory-to-memory DMA transfer demonstrator for STM ...
- STM32F1XX devices vector table for EWARM toolchain.
;******************** (C) COPYRIGHT 2014 STMicroelectronics ******************* ;* File Name : start ...
- 零宽断言 -- Lookahead/Lookahead Positive/Negative
http://www.vaikan.com/regular-expression-to-match-string-not-containing-a-word/ 经常我们会遇到想找出不包含某个字符串的文 ...
- C#打印图片
打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理.所以,不管用什么模板,什么方式:能在PrintPage事件处理中,生成一张要打印内容的图片就OK了! C#实现打印源码如下: ...