Share my C++ code 56ms


  • 0
    C
    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {
            bool num[9];
            for (int k = 0; k < 3; k++) {
                for (int i = 0; i < 9; i++) {
                    for (int j = 0; j < 9; j++) {
                        num[j] = false;
                    }
                    for (int j = 0; j < 9; j++) {
                        char c = getChar(i, j, k, board);
                        if (c == '.') {
                            continue;
                        }
                        if (num[c - '1']) {
                            return false;
                        } else {
                            num[c - '1'] = true;
                        }
                    }
                }
            }
            return true;
        }
    
    protected:
        static char getChar(int pi, int pj, int meth, vector<vector<char> > &board) {
            switch(meth) {
                case 0:
                    return board[pi][pj];
                case 1:
                    return board[pj][pi];
                case 2:
                    return board[pi / 3 * 3 + pj % 3][pi % 3 * 3 + pj / 3];
                default:
                    return '.';
            }
        }
    };

Log in to reply
 

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