当前位置: 代码迷 >> 综合 >> leetcode 36. Valid Sudoku (medium)
  详细解决方案

leetcode 36. Valid Sudoku (medium)

热度:52   发布时间:2024-01-05 00:32:14.0

实现题,注意细节和边界

class Solution
{
    private:bool checkid(char c, bool used[]){
    if (c == '.')return true;if (used[c - '1'])return false;return used[c - '1'] = true;}public:bool isValidSudoku(vector<vector<char>> &board){
    bool used[9];// 检查行列for (int i = 0; i < 9; i++){
    // 检查行.fill(used, used + 9, false);for (int j = 0; j < 9; j++){
    if (!checkid(board[i][j], used))return false;}// 检查列.fill(used, used + 9, false);for (int j = 0; j < 9; j++){
    if (!checkid(board[j][i], used))return false;}}// 检查格子for (int r = 0; r < 3; r++){
    for (int c = 0; c < 3; c++){
    fill(used, used + 9, false);for (int i = r * 3; i < r * 3 + 3; i++){
    for (int j = c * 3; j < c * 3 + 3; j++){
    if (!checkid(board[i][j], used))return false;}}}}return true;}
};
  相关解决方案