LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  bool isValidSudoku(vector<vector<char>>& board) {
    // 检查行
    for (auto& x : board) {
      unordered_map<char, int> m;
      for (auto& y : x) {
        if (y != '.') {
          if (m.count(y)) {
            return false;
          }
          ++m[y];
        }
      }
    }
    // 检查列
    for (int i = 0; i < 9; ++i) {
      unordered_map<char, int> m;
      for (int j = 0; j < 9; ++j) {
        char c = board[j][i];
        if (c != '.') {
          if (m.count(c)) {
            return false;
          }
          ++m[c];
        }
      }
    }
    // 检查九宫格
    for (int i = 0; i < 9; i += 3) {
      for (int j = 0; j < 9; j += 3) {
        unordered_map<char, int> m;
        for (int k = 0; k < 3; ++k) {
          for (int n = 0; n < 3; ++n) {
            char c = board[i + k][j + n];
            if (c != '.') {
              if (m.count(c)) {
                return false;
              }
              ++m[c];
            }
          }
        }
      }
    }
    return true;
  }
};