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

    package LeetCode_36
    
    /**
     * 36. Valid Sudoku
     * https://leetcode.com/problems/valid-sudoku/description/
     *
     * 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.
    
    Note:
    ==A Sudoku board (partially filled) could be valid but is not necessarily solvable.
    ==Only the filled cells need to be validated according to the mentioned rules.
    ==The given board contain only digits 1-9 and the character '.'.
    ==The given board size is always 9x9.
     * */
    class Solution {
        /*
        * solution: HashSet
        * */
        fun isValidSudoku(board: Array<CharArray>): Boolean {
            /*
            * for example:
            * [
              ["5","3",".",".","7",".",".",".","."],
              ["6",".",".","1","9","5",".",".","."],
              [".","9","8",".",".",".",".","6","."],
              ["8",".",".",".","6",".",".",".","3"],
              ["4",".",".","8",".","3",".",".","1"],
              ["7",".",".",".","2",".",".",".","6"],
              [".","6",".",".",".",".","2","8","."],
              [".",".",".","4","1","9",".",".","5"],
              [".",".",".",".","8",".",".","7","9"]
            ]
            * */
            val set = HashSet<Char>()
            val size = board.size
            val n = board[0].size
            for (i in 0 until size) {
                set.clear()
                //check the rows like:
                //["5","3",".",".","7",".",".",".","."],
                for (j in 0 until n) {
                    val char = board[i][j]
                    if (char != '.' && set.contains(char)) {
                        return false
                    }
                    set.add(char)
                }
                set.clear()
    
                //check the cols like:
                //["5","6",".","8","4","7",".",".","."],
                for (j in 0 until n) {
                    val char = board[j][i]
                    if (char != '.' && set.contains(char)) {
                        return false
                    }
                    set.add(char)
                }
                set.clear()
    
                //check each 3x3 sub-boxes
                /*like:
                * ["5","3",".",".","7",".",".",".","."],
                  ["6",".",".","1","9","5",".",".","."],
                  [".","9","8",".",".",".",".","6","."],
                  ==>
                  ["5","3",".","6",".",".",".","9","8"],
                * */
                val x = i / 3 * 3
                val y = i % 3 * 3
                for (j in 0 until n) {
                    val char = board[x + j / 3][y + j % 3]
                    if (char != '.' && set.contains(char)) {
                        return false
                    }
                    set.add(char)
                }
            }
            return true
        }
    }
  • 相关阅读:
    简单的理解原型链
    react->Context笔记
    工作上git指令小结
    vue 绑定事件如何传递参数的同时拿到事件对象
    vsCode卸载后重新安装,以前的插件有没有效果的解决方法
    mongo 分组 aggregation
    Redisson分布式锁原理
    Virtual server server already has a web module live-mix-1.0.2-t230 loaded at / therefore web module
    二进制中 1 的个数
    替换空格
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13252772.html
Copyright © 2011-2022 走看看