"036-valid Sudoku (verified sudoku chessboard)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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 ‘.‘
.
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.
Main Topic
Verify that a Sudoku chessboard is legal, and the validation rules for the Sudoku board are shown on the link's page.
The Sudoku chessboard is partially filled, with empty positions used instead of points.
Note: The legal chessboard does not necessarily require solvable, as long as the number of fills satisfies the requirements.
Thinking of solving problems
Check the rows first, then check the columns, and check the 3*3 squares.
Code Implementation
Algorithm implementation class
Public class solution { Public Boolean Isvalidsudoku(Char[] board) {//. The ASCII value is 46,0 ASCII value is 48,/ASCII value is intNumber = board[0].length;int[] record =New int[Ten+2];//save. To a value of 9, the location where the data is saved is in [2, ten] BooleanIsValid; Reset (record);//Check the line for(inti =0; I < number; i++) { for(intj =0; J < number; J + +) {Record[board[i][j]-'. ']++; }if(!check (record)) {//If check failed return false; }Else{//Check to reset the board successfullyReset (record); } }//Check the column for(inti =0; I < number; i++) { for(intj =0; J < number; J + +) {Record[board[j][i]-'. ']++; }if(!check (record)) {//If check failed return false; }Else{//Check to reset the board successfullyReset (record); } }//Check 3*3 block for(inti =0; I <3; i++) { for(intj =0; J <3; J + +) { for(intK = i *3; K < (i +1) *3; k++) { for(intL = J *3; L < (j +1) *3; l++) {record[board[k][l]-'. ']++; } }if(!check (record)) {//If check failed return false; }Else{//Check to reset the board successfullyReset (record); } } }return true; }Private void Reset(int[] a) { for(inti =0; i < a.length; i++) {A[i] =0; } }/** * Check the chessboard line, a column, or the 3*3 of the grid is legal, if the number in 1-9 is more than 1 is not legal * * @param A verification number * @return return results */ Private Boolean Check(int[] a) { for(inti =2; i < a.length; i++) {if(A[i] >1) {return false; } }return true; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47079373"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "036-valid Sudoku (verified sudoku Chessboard)"