• LeetCode之“散列表”: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.

      这道题解法暂没有比较巧妙的,所以下边程序所用方法就是Brute Force(暴力破解):

     1 class Solution {
     2 public:
     3     int charToInt(char c)
     4     {
     5         char str[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
     6         int intStr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
     7         for(int i = 0; i < 9; i++)
     8         {
     9             if(c == str[i])
    10                 return intStr[i];
    11         }
    12         return -1;
    13     }
    14     
    15     bool isValidSudoku(vector<vector<char>>& board) {
    16         unordered_map<int, int> hashMap;
    17         for(int i = 1; i < 10; i++)
    18             hashMap[i] = 0;
    19         // row by row
    20         for(int i = 0; i < 9; i++)
    21         {
    22             for(int k = 0; k < 10; k++)
    23                 hashMap[k] = 0;
    24             for(int j = 0; j < 9; j++)
    25             {
    26                 int tmp = charToInt(board[i][j]);
    27                 if(tmp != -1)
    28                 {
    29                     hashMap[tmp]++;
    30                     if(hashMap[tmp] > 1)
    31                         return false;
    32                 }
    33             }
    34         }
    35         // column by column
    36         for(int j = 0; j < 9; j++)
    37         {
    38             for(int k = 1; k < 10; k++)
    39                 hashMap[k] = 0;
    40             for(int i = 0; i < 9; i++)
    41             {
    42                 int tmp = charToInt(board[i][j]);
    43                 if(tmp != -1)
    44                 {
    45                     hashMap[tmp]++;
    46                     if(hashMap[tmp] > 1)
    47                         return false;
    48                 }
    49             }
    50         }
    51         // 3*3 boxes by 3*3 boxes
    52         for(int i = 0; i < 9; i += 3)
    53         {
    54             for(int j = 0; j < 9; j += 3)
    55             {
    56                 for(int k = 0; k < 10; k++)
    57                     hashMap[k] = 0;
    58                 for(int m = i; m < i + 3; m++)
    59                     for(int n = j; n < j + 3; n++)
    60                     {
    61                         int tmp = charToInt(board[m][n]);
    62                         if(tmp != -1)
    63                         {
    64                             hashMap[tmp]++;
    65                             if(hashMap[tmp] > 1)
    66                                 return false;
    67                         }
    68                     }
    69             }
    70         }
    71         
    72         return true;
    73     }
    74 };
  • 相关阅读:
    345. Reverse Vowels of a String
    344. Reverse String
    125. Valid Palindrome
    67. Add Binary
    28. Implement strStr()
    20. Valid Parentheses
    14. Longest Common Prefix
    670. Maximum Swap
    2017济南北大青鸟accp和学士后课程的真实情况
    2017济南北大青鸟accp和学士后课程的真实情况
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4590691.html
Copyright © 2020-2023  润新知