zoukankan      html  css  js  c++  java
  • [leetcode]_Valid Sudoku

    中间被启程日本的面试弄的没有静下心来复习算法。这样不好,基本功是硬道理。逐步恢复刷题。

    题目:给一个数独(九宫格)中的一些数字,判断该数独是否有效。

            即按照数独的规则,判断其行、列、小九格中是否有重复的数字。如有,即判断无效。

    直接给代码吧,长期没刷题,代码质量有所下降。

     1  public boolean isValidSudoku(char[][] board) {
     2         int[] help = new int[10];
     3         
     4         //横排检测
     5         for(int i = 0 ; i < 9 ; i++){
     6             for(int j = 0 ; j < 9 ; j++){
     7                 if(board[i][j] != '.') help[board[i][j] - '0']++;
     8             }
     9             for(int k = 1 ; k <= 9 ; k++){
    10                 if(help[k] > 1) return false;
    11                 else help[k] = 0;
    12             }
    13         }
    14         
    15         //竖排检测
    16         for(int row = 0 ; row < 9 ; row++){
    17             for(int col = 0 ; col < 9 ; col++){
    18                 if(board[col][row] != '.') help[board[col][row] - '0']++;
    19             }
    20             for(int k = 1 ; k <= 9 ; k++){
    21                 if(help[k] > 1) return false;
    22                 else help[k] = 0;
    23             }
    24         }
    25         
    26         //小九宫格检测
    27         int rowStart = 0,rowEnd = 3, colStart = 0 ,colEnd = 3;
    28         while(colStart < 9){
    29             while(rowStart < 9){
    30                 for(int row = rowStart ; row < rowEnd ; row++){
    31                     for(int col = colStart ; col < colEnd ; col++){
    32                         if(board[row][col] != '.') help[board[row][col] - '0']++;
    33                     }
    34                 }
    35                 for(int k = 1 ; k <= 9 ; k++){
    36                         if(help[k] > 1) return false;
    37                         else help[k] = 0;
    38                 }
    39                 rowStart += 3;
    40                 rowEnd += 3;
    41             }
    42             colStart += 3;
    43             colEnd += 3;
    44             rowStart = 0;
    45             rowEnd = 3;
    46         }
    47         return true;
    48     }
  • 相关阅读:
    dup和dup2
    cassandra nodetools
    python 之mechanize
    IDEA使用GsonFormat
    游标应用
    SQL 2005 with(nolock)详解
    SET NOCOUNT ON
    异常处理机制(Begin try Begin Catch)
    FILLFACTOR 作用 sql
    触发器语法
  • 原文地址:https://www.cnblogs.com/glamourousGirl/p/3742613.html
Copyright © 2011-2022 走看看