zoukankan      html  css  js  c++  java
  • [LC] 36. Valid Sudoku

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

    1. Each row must contain the digits 1-9 without repetition.
    2. Each column must contain the digits 1-9 without repetition.
    3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
    class Solution {
        public boolean isValidSudoku(char[][] board) {
            int n = board.length;
            for (int i = 0; i < n; i++) {
                Set<Character> rowSet = new HashSet<>();
                Set<Character> colSet = new HashSet<>();
                Set<Character> cubeSet = new HashSet<>();
                for (int j = 0; j < n; j++) {
                    if (board[i][j] != '.' && !rowSet.add(board[i][j])) {
                        return false;
                    }
                    if (board[j][i] != '.' && !colSet.add(board[j][i])) {
                        return false;
                    }
                    int rowIndex = 3 * (i / 3);
                    int colIndex = 3 * (i % 3);
                    
                    if (board[rowIndex + j / 3][colIndex + j % 3] != '.' && !cubeSet.add(board[rowIndex + j / 3][colIndex + j % 3])) {
                        return false;
                    }
                }
            }
            return true;
        }
    }
  • 相关阅读:
    Diameter 消息格式解析
    我们活成了不想的样子
    《活着》片段
    我的庚子年
    <<甄嬛传>>后感
    对于根目录磁盘满的了问题
    phpstorm注册账号
    mac安装nginx
    samba文件共享及账户映射
    我们的读书会
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12791621.html
Copyright © 2011-2022 走看看