leetcode36. Valid Sudoku

来源:互联网 发布:诺一网络水军 编辑:程序博客网 时间:2024/04/24 13:04

题目:

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.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

比较简单,只需要检查细节就行了

AC解:

class Solution {public:    bool isValidSudoku(vector<vector<char>>& board)     {        bool used[10];               //检查行        for (int i = 0; i < 9; i++)        {            fill(used,used + 10,false);            for (int j = 0; j < 9; j++)            {                if (!check(used,board[i][j]))                    return false;            }        }        //检查列        for (int i = 0; i < 9; i++)        {            fill(used,used + 10,false);            for (int j = 0; j < 9; j++)                if (!check(used,board[j][i]))                    return false;        }        //检查九方格        for (int i = 0; i < 3; i++)            for (int j = 0; j < 3; j++)            {                fill(used,used + 10,false);                for (int m = 3 * i; m < 3 * i + 3; m++)                    for (int n = 3 * j; n < 3 * j + 3; n++)                        if (!check(used,board[m][n]))                            return false;            }                return true;    }        bool check(bool used [],char a)    {        if (a == '.')    return true;        if (used[a - '0']) return false;                used[a - '0'] = true;        return true;    }};



0 0
原创粉丝点击