Valid Sudoku
Total Accepted: 33785 Total Submissions: 123877 My Submissions
Question Solution
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is filled with the character ‘.‘
.
650) this.width=650; "Src=" http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/ 250px-sudoku-by-l2g-20050714.svg.png "style=" border:0px;vertical-align:middle; "alt=" 250px-sudoku-by-l2g-20050714.svg.png "/>
A partially filled sudoku which is valid.
Analysis: Each element in each row can be empty, but the 0~9 number only appears once;
Each element in each column can be empty, but the 0~9 number only appears once;
Each element in a small nine can be empty, but the 0~9 number only appears once;
public class Solution {
Boolean IsV (int xs, int xe, int ys, int ye, char[][] board)
{
Map<character,integer> z=new hashmap<character,integer> ();
for (int i=xs;i<=xe;i++)
for (int j=ys;j<=ye;j++)
if (board[i][j]>= ' 0 ' &&board[i][j]<= ' 9 ')
if (Z.get (Board[i][j])!=null)
return false;
Else
Z.put (board[i][j],1);
return true;
}
public boolean Isvalidsudoku (char[][] board) {
Map<character,integer> x;
Map<character,integer> y;
for (int i=0;i<9;i++)
{
X=new hashmap<character,integer> ();
Y=new hashmap<character,integer> ();
for (int j=0;j<9;j++)
{
if (board[i][j]>= ' 0 ' &&board[i][j]<= ' 9 ')
if (X.get (Board[i][j])!=null)
return false;
Else
X.put (board[i][j],1);
if (board[j][i]>= ' 0 ' &&board[j][i]<= ' 9 ')
if (Y.get (Board[j][i])!=null)
return false;
Else
Y.put (board[j][i],1);
}
X.clear ();
Y.clear ();
}
if (IsV (0,2,0,2,board) &&isv (3,5,0,2,board) &&isv (6,8,0,2,board) &&ISV (0,2,3,5,board) & &ISV (3,5,3,5,board) &&isv (6,8,3,5,board) &&isv (0,2,6,8,board) &&ISV (3,5,6,8,board) &&ISV (6,8,6,8,board))
return true;
Else
return false;
}
}
Leetcode#36valid Sudoku