Valid Sudoku

来源:互联网 发布:网络阅卷成绩 编辑:程序博客网 时间:2024/06/01 08:51

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.

public class Solution {    public boolean isValidSudoku(char[][] board) {            boolean [][] row = new boolean[9][9];            boolean [][] column = new boolean[9][9];            boolean [][] block = new boolean[9][9];            for(int i=0;i<9;i++)            {                for (int j = 0;j<9;j++)                {                    int c = board[i][j]-'1';                    if(board[i][j]=='.')                        continue;                    if(row[i][c]||column[j][c]||block[i-i%3+j/3][c])    return false;                    row[i][c]=column[j][c]=block[i-i%3+j/3][c] = true;                }            }            return true;                    }}



0 0
原创粉丝点击