Did anyone already post this simple O(n2) C++ code?


  • 0
    G
        bool isValidSudoku(vector<vector<char>>& board) {
            int r_used[9][9] = {0}, c_used[9][9] = {0}, s_used[9][9] = {0};
            for (int i = 0; i < board.size(); i++) {
                for (int j = 0; j < board[0].size(); j++) {
                    if (board[i][j] == '.') {
                        continue;
                    }
                    int n = board[i][j] - '0' - 1;
                    int k = (i / 3) * 3 + (j / 3);
                    if (r_used[i][n] + c_used[j][n] + s_used[k][n] != 0) {
                        return false;
                    }
                    r_used[i][n] = c_used[j][n] = s_used[k][n] = 1;
                }
            }
            return true;
        }
    

Log in to reply
 

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