有效的数独 (Valid Sudoku)

 

思路:

// @Title: 有效的数独 (Valid Sudoku)
// @Author: qisiii
// @Date: 2024-06-13 19:21:37
// @Runtime: 2 ms
// @Memory: 43.4 MB
// @comment: 
// @flag: 
class Solution {
    HashSet<Character> set=new HashSet<>();
    HashSet<Character> setColumn=new HashSet<>();
    int[][][] nine=new int[3][3][9];
    public boolean isValidSudoku(char[][] board) {
       boolean row= valid(board);
       if(!row){
        return false;
       }
        return true;
    }
    public boolean valid(char[][] board){
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]!='.'){
                    if(!set.contains(board[i][j])){
                        set.add(board[i][j]);
                    }else{
                        return false;
                    }
                   
                }
                if(board[j][i]!='.'){
                    if(!setColumn.contains(board[j][i])){
                        setColumn.add(board[j][i]);
                    }else{
                        return false;
                    }
                   
                }
                if(board[i][j]!='.'){
                    int val=board[i][j]-'0'-1;
                    nine[i/3][j/3][val]++;
                    if(nine[i/3][j/3][val]>1){
                        return false;
                    }
                }

            }
            set.clear();
            setColumn.clear();
        }
        return true;
    }
    
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18