* 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.

 1 public class Solution {
 2 public boolean isValidSudoku(char[][] board) {
 3     for (int i=0; i<9; i++) {
 4         if (!isParticallyValid(board,i,0,i,8)) return false;
 5         if (!isParticallyValid(board,0,i,8,i)) return false;
 6     }
 7     for (int i=0;i<3;i++){
 8         for(int j=0;j<3;j++){
 9             if (!isParticallyValid(board,i*3,j*3,i*3+2,j*3+2)) return false;
10         }
11     }
12     return true;
13 }
14 private boolean isParticallyValid(char[][] board, int x1, int y1,int x2,int y2){
15     Set singleSet = new HashSet();
16     for (int i= x1; i<=x2; i++){
17         for (int j=y1;j<=y2; j++){
18             if (board[i][j]!='.') if(!singleSet.add(board[i][j])) return false; // If this set already contains the element, the call leaves the set unchanged and returns false.
19         }
20     }
21     return true;
22 }
23 
24 }

https://leetcode.com/discuss/17990/sharing-my-easy-understand-java-solution-using-set

原文地址:https://www.cnblogs.com/hygeia/p/5074821.html