My cpp solution to Valid Sudoku


  • 0
    X

    class Solution {
    public:
    bool isValidSudoku(std::vector<std::vector<char> > &board)
    {
    int len = board.size();
    if (len != 9) return false;
    bool row_check[9];
    bool col_check[9];
    bool subBox_check[9];
    for (int row = 0; row < len; ++row)
    {
    std::fill(row_check, row_check+9, false);
    std::fill(col_check, col_check+9, false);
    std::fill(subBox_check, subBox_check+9, false);
    for (int col = 0; col < len; ++col)
    {
    if (!Solution::if_valid(board, row, col, row_check)){
    return false;
    }
    if (!Solution::if_valid(board, col, row, col_check)){
    return false;
    }
    if (!Solution::if_valid(board, 3*(row/3)+col/3, 3*(row%3)+col%3, subBox_check)){
    return false;
    }
    }
    }
    return true;
    }
    static bool if_valid(std::vector<std::vector<char> > &board, int x, int y, bool (&some_check)[9])
    {
    if (board[x][y]=='.'){
    return true;
    }
    else{
    if (some_check[board[x][y]-'1']){
    return false;
    }
    else{
    some_check[board[x][y]-'1'] = true;
    return true;
    }
    }
    return false;
    }
    };


Log in to reply
 

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