36.判断数独是否合法

Valid Sudoku

问题描述:

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
数独

A partially filled sudoku which is valid.

测试代码:

    bool isValidSudoku(vector<vector<char>>& board) {
        int row[9][9]={0},column[9][9]={0},square[9][9]={0};
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                if(board[i][j]!='.')
                {
                    int k = i/3*3+j/3;
                    int number = board[i][j]-'0'-1;
                    if(row[i][number]||column[j][number]||square[k][number])
                    {
                        return false;
                    }
                    row[i][number]=column[j][number]=square[k][number]=1;

                }
            }
        }
        return true;

    }

性能:

36.判断数独是否合法_第1张图片

你可能感兴趣的:(LeetCode,C++,LeetCode,数独)