4ms C solution easy to read with comments.


  • -1
    C

    basically, we just check rows, cols, and sub area accordingly.

    use bool cache[10] to check if number is taken. return false if number is taken previously. otherwise, save it and check later.

    cellVal() function converts char to int to fit in cache[].

    int cellVal(char c)
    {
        if (c -'0' <= 9 && c -'0' >= 0)
        {
            return c -'0';
        }
        else if (c == '.')
        {
            return -1;
        }
        
        return -1;
    }
    
    bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
        
        int i, j;
        bool cache[10] = {0};
        int divI = boardRowSize/3;
        int divJ = boardColSize/3;
        int di, dj;
        int count = 0;
        
        // check subs
        for (di=0; di<divI; di++)
        {
            for (dj=0; dj<divJ; dj++)
            {
                for (i=(di*3); i<(di*3+3); i++)
                {
                    for (j=(dj*3); j<(dj*3+3); j++)
                    {
                        int val = cellVal(board[i][j]);
    
                        if (val >= 0)
                        {
                            if (cache[val] == 1)
                            {
                                return false;
                            }
                            else
                            {
                               cache[val] = 1; 
                            }
                        }
                        
                        if (++count == 9)
                        {
                            memset(cache, 0, 10);
                            count = 0;
                        }
                    }
                }
            }
        }
        
        // check rows
        for (i=0; i<boardRowSize; i++)
        {
            for (j=0; j<boardColSize; j++)
            {
                int val = cellVal(board[i][j]);
                
                if (val >= 0)
                {
                    if (cache[val] == 1)
                    {
                        return false;
                    }
                    else
                    {
                       cache[val] = 1; 
                    }                
                }
                
                if (++count == 9)
                {
                    memset(cache, 0, 10);
                    count = 0;
                }
            }
        }
        
        // check cols
        for (j=0; j<boardColSize; j++)
        {
            for (i=0; i<boardRowSize; i++)
            {
                int val = cellVal(board[i][j]);
                
                if (val >= 0)
                {
                    if (cache[val] == 1)
                    {
                        return false;
                    }
                    else
                    {
                       cache[val] = 1; 
                    }                
                }
                
                if (++count == 9)
                {
                    memset(cache, 0, 10);
                    count = 0;
                }            
            }
        }
        
        return true;
    }

Log in to reply
 

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