Why my C++ solution runs 19ms using map and 2 loops but not less time?


  • 0
    V

    The sample solution using the same time as me uses 2 loops respectively for checking rows, columns and regions.
    I want to know if my solution can be optimized (sorry I speak poor English..

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            int index[9] = {0, 0, 0, 1, 1, 1, 2, 2, 2};
            map<char, int> m[3][3], row[9], col[9];
            for (int i = 0; i < board.size(); ++i) {
                for (int j = 0; j < board.size(); ++j) {
                    if (board[i][j] == '.')
                        continue;
                    if (++m[index[i]][index[j]][board[i][j]] == 2)
                        return false;
                    if (++row[i][board[i][j]] == 2)
                        return false;
                    if (++col[j][board[i][j]] == 2)
                        return false;
                    
                }
            }
            return true;
        }
    };
    

Log in to reply
 

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