• Valid Sudoku


    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

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

    A partially filled sudoku which is valid.

    Note:
    A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

    分析:检验数独是否有效。只用检查已经填充的部分,没填充的部分用 '.' 表示。

    分别检查行、列以及小九宫格。为了避免麻烦,我用了map来迅速查找数字。运行时间46ms。

     1 class Solution {
     2 public:
     3     bool isValidSudoku(vector<vector<char>>& board) {
     4         for(int i = 0; i < 9; i++){
     5             //check each row
     6             map<char, int> m;
     7             for(int j = 0; j < 9; j++){
     8                 if(board[i][j] != '.'){
     9                     if(m.find(board[i][j]) != m.end()) return false;
    10                     else m[board[i][j]] = 1;
    11                 }
    12             }
    13             
    14             //check each column
    15             map<char, int> n;
    16             for(int k = 0; k < 9; k++){
    17                 if(board[k][i] != '.'){
    18                     if(n.find(board[k][i]) != n.end()) return false;
    19                     else n[board[k][i]] = 1;
    20                 }
    21             }
    22         }
    23         //check each sub-boxes
    24         for(int i = 0; i < 9; i += 3){
    25             for(int j = 0; j < 9; j += 3){
    26                 map<char, int> mm;
    27                 for(int p = i; p < i + 3; p++){
    28                     for(int q = j; q < j + 3; q++){
    29                         if(board[p][q] != '.'){
    30                             if(mm.find(board[p][q]) != mm.end()) return false;
    31                             else{
    32                                 mm[board[p][q]] = 1;
    33                             }
    34                         }
    35                     }
    36                 }
    37             }
    38         }
    39         return true;
    40     }
    41 };
    View Code

     为了进一步优化,可以不用map进行数字重复测试。通过使用一个bool型数组,每出现一个数字,便将该数字相应下标置1,运行时间16ms!

     1 class Solution {
     2 public:
     3     bool isValidSudoku(vector<vector<char>>& board) {
     4         for(int i = 0; i < 9; i++){
     5             //check each row
     6             bool arrRow[9];
     7             fill(arrRow, arrRow + 9, false);
     8             for(int j = 0; j < 9; j++){
     9                 if(board[i][j] != '.'){
    10                     if(arrRow[board[i][j] - '1']) return false;
    11                     else arrRow[board[i][j] - '1'] = true;
    12                 }
    13             }
    14             
    15             //check each column
    16             bool arrCol[9];
    17             fill(arrCol, arrCol + 9, false);
    18             for(int k = 0; k < 9; k++){
    19                 if(board[k][i] != '.'){
    20                     if(arrCol[board[k][i] - '1']) return false;
    21                     else arrCol[board[k][i] - '1'] = true;
    22                 }
    23             }
    24         }
    25         //check each sub-boxes
    26         for(int i = 0; i < 9; i += 3){
    27             for(int j = 0; j < 9; j += 3){
    28                 bool arr[9];
    29                 fill(arr, arr + 9, false);
    30                 for(int p = i; p < i + 3; p++){
    31                     for(int q = j; q < j + 3; q++){
    32                         if(board[p][q] != '.'){
    33                             if(arr[board[p][q] - '1']) return false;
    34                             else arr[board[p][q] - '1'] = true;
    35                         }
    36                     }
    37                 }
    38             }
    39         }
    40         return true;
    41     }
    42 };
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    你会卖掉自己的网上信息吗?大数据可能根本不属于你
    机器学习——TensorFLow实战房价预测
    数据库运作实践三三之歌(秘制口诀)
    1000行MySQL学习笔记,收藏版!
    吐血整理深度学习入门路线及导航【教学视频+大神博客+书籍整理】+【资源页】(2019年已经最后一个月了,你还不学深度学习吗???)
    Ubuntu Snap 简述
    参数传递
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4500985.html
Copyright © 2020-2023  润新知