zoukankan      html  css  js  c++  java
  • leetcode114:valid-sudoku

    题目描述

    根据数独的规则Sudoku Puzzles - The Rules.判断给出的局面是不是一个符合规则的数独局面
    数独盘面可以被部分填写,空的位置用字符'.'.表示
    这是一个部分填写的符合规则的数独局面

    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.


    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {
            //记录已经出现的数,如3出现,则row[3]=1
            int row[10] = {0};//之所以是10,因为下标最大是9
            int col[9][10] = {0};
            int square[9][10] = {0};
             
            //行遍历,两层循环即可,只要找到重复的数即返回false
            for(int i=0; i<9; i++){
                //由于行重复使用了,所以每次都要清空
                memset(row, 0, sizeof(row));
                for(int j=0; j<9; j++){
                    if(board[i][j] != '.'){
                        if(!check(row, board[i][j] - '0') ||
                           !check(col[j], board[i][j] - '0') ||
                           //九宫格的序号:i/3*3 + j/3
                           !check(square[i/3*3 + j/3], board[i][j] - '0')                
                          )
                            return false;
                    }
                }
            }
             
            return true;       
        }
         
        bool check(int a[], int v){
            //如果每行或每列或每个九宫格出现了重复的数,那么返回false
            if(a[v] == 1) return false;
            a[v] = 1;
            return true;
        }
    };

  • 相关阅读:
    挖矿病毒入侵-分析总结
    Linux查看包依赖关系的神器-repoquery分享
    Elasticsearch 字段为空(null)记录查询
    Python 导数 Elasticsearch 元数据到CSV
    基于docker快速构建MySQL主从复制环境
    Redis环境简单部署(集群/双机)
    FTP 脚本 to Shell脚本&bat脚本&python脚本
    专用服务器模式&共享服务器模式
    CentOS 7安装部署ELK 6.2.4-SUCCESS
    zabbix 数据库迁移变更
  • 原文地址:https://www.cnblogs.com/hrnn/p/13414980.html
Copyright © 2011-2022 走看看