What's wrong with my code??


  • 0
    J

    my code is failing on this test case, can someone take a look and tell me why, I've been staring at it for 30 mins..

    [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            bool hash[9] = {false};
            // check row
            for (int i = 0; i < 9; ++i) {
                memset(hash, false, 9);
                for (int j = 0; j < 9; ++j) {
                    if (board[i][j] == '.') continue;
                    int index = board[i][j] - '0';
                    hash[index] = !hash[index];
                    if(hash[index] == false) {
                        return false;
                    }
                }
                
            }
            
            for (int i = 0; i < 9; ++i) {
                memset(hash, false, 9);
                for (int j = 0; j < 9; ++j) {
                    if (board[i][j] == '.') continue;
                    int index = board[i][j] - '0';
                    hash[index] = !hash[index];
                    if(hash[index] == false) {
                        return false;
                    }
                }
            }
            
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    memset(hash, false, 9);
                    for (int m = i; m < 3; ++m) {
                        for (int n = j; n < 3; ++n) {
                            if (board[i][j] == '.') continue;
                            int index = board[i][j] - '0';
                            hash[index] = !hash[index];
                            if(hash[index] == false) {
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
    };

Log in to reply
 

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