c++ (12 ms)


  • 0
    class Solution {
      public:
        int ch(char a) {
          return (a - '0');
        }
        bool isValidSudoku(vector<vector<char>>& board) {
          vector<bool> hash(9);
          for (int i = 0; i < 9; ++i) {
            hash.assign(9, false);
            for (int j = 0; j < 9; ++j) {
              if (board[i][j] == '.') continue;
              int t = ch(board[i][j]);
              if (hash[t]) return false;
              else hash[t] = true;
            }
            hash.assign(9, false);
            for (int j = 0; j < 9; ++j) {
              if (board[j][i] == '.') continue;
              int t = ch(board[j][i]);
              if (hash[t]) return false;
              else hash[t] = true;
            }
          }
          for (int i = 0; i < 7; i += 3) {
            for (int j = 0; j < 7; j += 3) {
              int n = i + 3, m = j + 3;
              hash.assign(9, false);
              for (int l = i; l < n; ++l) {
                for (int k = j; k < m; ++k) {
                  if (board[l][k] == '.') continue;
                  int t = ch(board[l][k]);
                  if (hash[t]) return false;
                  else hash[t] = true;
                }
              }
            }
          }
          return true;
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.