Judging whether Sudoku is legalPlease determine if a Sudoku is valid.
The Sudoku may only be populated with partial numbers, where missing numbers are . represented.
Sample Example
The following is an example of a legal sudoku.
Note
A valid Sudoku (partial padding only) is not necessarily solvable. We just need to make the padding space valid.
Description
What's that 数独 ?
- Http://sudoku.com.au/TheRules.aspx
- Http://baike.baidu.com/subview/961/10842669.htm
The idea of a time-out in the first place would be to have a space for a moment. The following code appears:
After a thought, should not time out to put. But this code is actually a lot simpler.
Total time: 1005 ms
1 classSolution {2 /**3 * @paramboard:the Board4 @return: Wether the Sudoku is valid5 */6 Public BooleanIsvalidsudoku (Char[] board) {7 intR[][] =New int[9] [9];8 intL[][] =New int[9] [9];9 intA[][][] =New int[3] [3] [9];Ten One for(inti=0;i<9;i++) { A for(intj=0;j<9;j++) { -R[I][J] = 0; -L[I][J] = 0; the if(I < 3 && J < 3) { - for(intk = 0; k<9; k++) -A[i][j][k] = 0; - } + } - } + A for(inti=0;i<9;i++) { at for(intj=0;j<9;j++) { - if(Board[i][j] = = '. ')Continue; - intn = board[i][j]-' 0 '; - if(N < 0 | | n > 9)return false; - if(++r[i][n-1] > 1)return false; - if(++l[j][n-1] > 1)return false; in intx = I/3; - inty = J/3; to if(++a[x][y][n-1] > 1)return false; + } - } the * return true; $ }Panax Notoginseng};View Code
Judging whether Sudoku is legal (Lintcode)