Valid Sudoku

Determine whether a Sudoku is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character ..

 Notice

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

Example

The following partially filed sudoku is valid.

分析:

 1 class Solution {
 2     public boolean isValidSudoku(char[][] board) {
 3         Set<String> seen = new HashSet<>();
 4         for (int i = 0; i < 9; ++i) {
 5             for (int j = 0; j < 9; ++j) {
 6                 char number = board[i][j];
 7                 if (number != '.')
 8                     if (!seen.add(number + " in row " + i)
 9                             || !seen.add(number + " in column " + j)
10                             || !seen.add(number + " in block " + i / 3 + "-" + j / 3))
11                         return false;
12             }
13         }
14         return true;
15     }
16 }

 

posted @ 2016-07-13 12:02  北叶青藤  阅读(203)  评论(0编辑  收藏  举报