zoukankan      html  css  js  c++  java
  • 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.


    A partially filled sudoku which is valid.

    The Sudoku board could be partially filled, where empty cells are filled with the character '.'

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道怎么判断每个单独的3*3: 三倍行数遍历+一倍列数遍历进入具体的3*3

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    每一行都新建三个集合,然后行重复为[i][j] 列重复为[j][i]

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

     三倍行数遍历+一倍列数遍历进入具体的3*3

    [复杂度]:Time complexity: O(mn) Space complexity: O(mn)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    class Solution {
        public boolean isValidSudoku(char[][] board) {
            
            //for loop, judge row, col, and box
            for (int i = 0; i < 9; i++) {
                //initialization: 3 sets in each new row
                HashSet<Character> row = new HashSet<>();
                HashSet<Character> col = new HashSet<>();
                HashSet<Character> box = new HashSet<>();
                
                for (int j = 0; j < 9; j++) {
                    //judge row, col
                    if (board[i][j] != '.' && !row.add(board[i][j])) return false;
                    if (board[j][i] != '.' && !col.add(board[j][i])) return false;
                    //judge box
                    //same for each row
                    int rowIndex = 3 * (i / 3);
                    System.out.println("rowIndex = " + rowIndex);
                    int colIndex = 3 * (i % 3);
                    System.out.println("colIndex = " + colIndex);
                    System.out.println("-------------");
                    //different for each col
                    if (board[rowIndex + j / 3][colIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][colIndex + j % 3])) return false;
                }
            }
        
            return true;
        }
    }
    View Code
  • 相关阅读:
    三极管8050和8550对管的参数
    三极管9014 管脚
    水深不语,人稳不言
    编译结果分析
    三母运算符
    C语言关键词解释
    51定时器初值的计算
    聪明人都在远离手机虚假繁荣的“人脉”关系
    每段路,都是一种领悟
    你的灯亮着吗读后感二
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9408169.html
Copyright © 2011-2022 走看看