LeetCode: Valid Sudoku 解题报告

Valid Sudoku
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.

SOLUTION:

使用HashSet 行列,9块分别检查。

 1 public class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         if (board == null || board.length != 9 ||
 4             board[0].length != 9) {
 5             return false;
 6         }
 7
 8         HashSet<Character> set = new HashSet<Character>();
 9
10         // check the rows.
11         for (int i = 0; i < 9; i++) {
12             // clear the set at every row.
13             set.clear();
14             for (int j = 0; j < 9; j++) {
15                 if (!isValidChar(board[i][j], set)) {
16                     return false;
17                 }
18             }
19         }
20
21         // check the columns.
22         for (int i = 0; i < 9; i++) {
23             // clear the set at every column.
24             set.clear();
25             for (int j = 0; j < 9; j++) {
26                 if (!isValidChar(board[j][i], set)) {
27                     return false;
28                 }
29             }
30         }
31
32         // check the blocks.
33         for (int i = 0; i < 9; i+=3) {
34             for (int j = 0; j < 9; j+=3) {
35                 // clear the set at every block.
36                 set.clear();
37                 for (int k = 0; k < 9; k++) {
38                     if (!isValidChar(board[i + k / 3][j + k % 3], set)) {
39                         return false;
40                     }
41                 }
42             }
43         }
44
45         return true;
46     }
47
48     public boolean isValidChar(char c, HashSet<Character> set) {
49         if (c == ‘.‘) {
50             return true;
51         }
52
53         if (c < ‘0‘ || c > ‘9‘) {
54             return false;
55         }
56
57         // Check if the character exit in the hashset.
58         if (set.contains(c)) {
59             return false;
60         }
61
62         set.add(c);
63
64         return true;
65     }
66 }

主页君的GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/IsValidSudoku.java

ref: http://www.ninechapter.com/solutions/valid-sudoku/

时间: 2024-10-10 01:58:00

LeetCode: Valid Sudoku 解题报告的相关文章

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] 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 '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

LeetCode: Valid Parentheses 解题报告

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

LeetCode: Valid Palindrome 解题报告

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

[LeetCode]Valid Sudoku解题记录

这道题考查对二维数组的处理,哈希表. 1.最自然的方法就是分别看每个数是否符合三个规则,所以就需要相应的数据结构来 记录这些信息,判定是否存在,显然最先想到用哈希表. 2.学会把问题抽象成一个个的子问题. 3.在索引的构建上下工夫. 4.底层数组如何对应的细节没有那么重要,重要的是构成了问题的全集. 代码:这里 附图:一趟遍历时根据i,j,对应到具体的grid,这里的构造模式有多种(??)

[leetcode]Valid Anagram解题报告 C语言

[题目] Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may assume the string cont

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

[leetcode]Valid Sudoku @ Python

原题地址:https://oj.leetcode.com/problems/valid-sudoku/ 题意: 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 sudo

LeetCode: Valid Sudoku [035]

[题目] 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 (part