zoukankan      html  css  js  c++  java
  • 36. 有效的数独

    题目

    代码

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            //判断9宫格
            for(int i=1;i<8;i+=3)
            {
                for(int j=1;j<8;j+=3)
                {
                    std::map<char,int> table;
                    table[ board[i-1][j-1]]++;
                    table[ board[i-1][j]]++;
                    table[ board[i-1][j+1]]++;
                    table[ board[i][j-1]]++;
                    table[ board[i][j]]++;
                    table[ board[i][j+1]]++;
                    table[ board[i+1][j-1]]++;
                    table[ board[i+1][j]]++;
                    table[ board[i+1][j+1]]++;
                    for(auto i:table)
                    {
                        if(i.first!='.'&&i.second>1)
                            return false;
                    }
                }
            }
            //判断水平
            
        
            for(int i=0;i<board.size();i++)
            {
                std::map<char,int> hori;
                for(int j=0;j<board[0].size();j++)
                {
                     hori[board[i][j]]++;
                }
                for(auto i:hori)
                {
                    if(i.first!='.'&&i.second>1)
                        return false;
                }
               
            }
            //判断竖直
            for(int j=0;j<board.size();j++)
            {
                   std::map<char,int> verti;
                for(int i=0;i<board[0].size();i++)
                {
                     verti[board[i][j]]++;
                }
                for(auto temp:verti)
                {
                    if(temp.first!='.'&&temp.second>1)
                        return false;
                }
               
            }
            return true;
        }
    };

    思路

    暴力破解法,直接按照规则判断九宫格和每个横线和竖线上的数字是否符合规则。时间复杂度为O(n²)

    https://github.com/li-zheng-hao
  • 相关阅读:
    随机数测试
    往xml中更新节点
    Spring学习之代理
    SpringMVC基本配置
    Hibernate映射一对一关联关系
    成员变量的定义与使用
    面向对象三大特性
    请用心“品尝”网络电视精灵
    汽车租赁系统
    JSP 甜点
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053647.html
Copyright © 2011-2022 走看看