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;
        }
    }
  • 相关阅读:
    三十二、恢复单库单表
    三十一、XBK备份
    三十、分库分表备份脚本
    二十九、mysqldump恢复案例
    二十八、mysqldump备份
    二十七、备份介绍
    二十六:慢日志分析
    二十五、二进制日志之GTID模式
    Trie树
    AC自动机
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12791621.html
Copyright © 2011-2022 走看看