前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode题目036. 有效的数独

Leetcode题目036. 有效的数独

作者头像
用户6021899
发布2023-03-03 09:05:22
1300
发布2023-03-03 09:05:22
举报

思路简单直接,直接上C++代码

代码语言:javascript
复制
class Solution
{
public:
    bool isValidSudoku(vector<vector<char>>& board)
{
        
        for (int i = 0; i < 9; i++) //每一行中不能有重复
        {
            unordered_set<char> used_chars;
            for (int j = 0; j < 9; j++)
            {
                char ch = board[i][j];
                if(ch == '.') continue;
                if (used_chars.find(ch) != used_chars.end()) //能找到!
                    return false;
                else
                    used_chars.insert(ch);
            }
        }

        for (int j = 0; j < 9; j++) //每一列中不能有重复
        {
            unordered_set<char> used_chars;
            for (int i = 0; i < 9; i++)
            {
                char ch = board[i][j];
                if(ch == '.') continue;
                if (used_chars.find(ch) != used_chars.end()) //能找到!
                    return false;
                else
                    used_chars.insert(ch);
            }
        }

        for(int R=0; R<3; R++) //每一个以粗实线分隔的 3x3 宫内不能有重复
        {
            for(int C=0; C<3; C++)
            {
                unordered_set<char> used_chars;
                for(int i = 3 * R; i < 3 * R + 3; i++)
                {
                    for (int j = 3 * C; j < 3 * C + 3; j++)
                    {
                        char ch = board[i][j];
                        if(ch == '.') continue;
                        if (used_chars.find(ch) != used_chars.end()) //能找到!
                            return false;
                        else
                            used_chars.insert(ch);
                    }
                }
            }
        }

        return true;
    }

};
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档