Share my 8ms c++ solution, the code is a little bit long but not complicated


  • 0
    P
    bool isValidSudoku(vector<vector<char>>& board) {
        for (int i = 0; i < 9; i++) {
            int v_nine[9] = {0};
            int h_nine[9] = {0};
            for (int j = 0; j < 9; j++) {
                char v_c = board[i][j];
                if (v_c != '.') {
                    if (v_nine[v_c-'1']) {
                        return false;
                    }
                    v_nine[v_c-'1'] = 1;
                }
                char h_c = board[j][i];
                if (h_c != '.') {
                    if (h_nine[h_c-'1']) {
                        return false;
                    }
                    h_nine[h_c-'1'] = 1;
                }
            }
        }
        
        int is = 0, ie = 3, js = 0, je = 3;
        while (ie < 10) {
            int nine[9] = {0};
            for (int i = is; i < ie; i++) {
                for (int j = js; j < je; j++) {
                    char c = board[i][j];
                    if (c != '.') {
                        if (c-'1' == 5) {
                            int x = 1;
                        }
                        if (nine[c-'1']) {
                            return false;
                        }
                        nine[c-'1'] = 1;
                    }
                }
            }
            if (je == 9) {
                is += 3;
                ie += 3;
                js = 0;
                je = 3;
            } else {
                js += 3;
                je += 3;
            }
        }
        return true;
    }

Log in to reply
 

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