Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
在这里插入图片描述
在这里插入图片描述

此题要求判断一个数独是否有效?
规则:
同一行,同一列,同一个33小方块都只含1-9中的不重复数字。解法也按照规则来,判断同行同列同33小方块内是否有重复数字,若无则ok,否则不行。

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        set<string> s;
        string s1="in row";
        string s2="in col";
        string s3="in square";
        for (int i=0;i<board.size();i++)
        {
            for (int j=0;j<board[0].size();j++)
            {
                char ch=board[i][j];
                if(ch!='.')
                {
                    cout<<ch<<endl;
                    string row=ch+s1+to_string(i),col=ch+s2+to_string(j),square=ch+s3+to_string(i/3)+to_string(j/3);
                    //cout<<row<<col<<square<<endl;
                    if(s.count(row)||s.count(col)||s.count(square))
                    {
                        return false;
                    }
                    else
                    {
                        s.insert(row);
                        s.insert(col);
                        s.insert(square);
                    }
                }
            }
        }
        return true;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值