leetcode---Valid Sudoku

来源:互联网 发布:mysql hint 用法 编辑:程序博客网 时间:2024/05/16 03:36

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 ‘.’.

这里写图片描述

class Solution {public:    bool isValidSudoku(vector<vector<char>>& board)     {        int cnt[10];        for(int i=0; i<9; i++) //每一行        {            memset(cnt, 0, sizeof(cnt));            for(int j=0; j<9; j++)            {                if(board[i][j] != '.')                {                    cnt[board[i][j]-'0']++;                    if(cnt[board[i][j]-'0'] > 1)                        return false;                }            }        }        for(int i=0; i<9; i++) //每一列        {            memset(cnt, 0, sizeof(cnt));            for(int j=0; j<9; j++)            {                if(board[j][i] != '.')                {                    cnt[board[j][i]-'0']++;                    if(cnt[board[j][i]-'0'] > 1)                        return false;                }            }        }        for(int i=0; i<9; i+=3) //每个宫        {            for(int j=0; j<9; j+=3)            {                memset(cnt, 0, sizeof(cnt));                for(int a=i; a<i+3; a++)                {                    for(int b=j; b<j+3; b++)                    {                        if(board[a][b] != '.')                        {                            cnt[board[a][b]-'0']++;                            if(cnt[board[a][b]-'0'] > 1)                                return false;                        }                    }                }            }        }        return true;    }};
0 0