zoukankan      html  css  js  c++  java
  • Leetcode Valid Sudoku

    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.

    Note:
    A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

    玩过九宫格的都应该知道规则(没玩过的可以试玩一下九宫格

    (1)每行1~9各出现一次

    (2)每列1~9各出现一次

    (3)每个小的3宫格,1~9各出现一次

    class Solution {
    public:
    
        bool isValidRow(vector<vector<char> >& board){
            for(int row = 0; row < 9; ++ row){
                vector<int> cnt(10,0);
                for(int col = 0; col < 9; ++ col){
                    char item = board[row][col];
                    if(item != '.'){
                        if(cnt[item-'0']!=0) return false;
                        else cnt[item-'0']++;
                    }
                }
            }
            return true;
        }
        
        bool isValidCol(vector<vector<char> >& board ){
            for(int col = 0; col < 9; ++ col){
                vector<int> cnt(10,0);
                for(int row = 0; row < 9; ++ row){
                    char item = board[row][col];
                    if(item != '.'){
                        if(cnt[item-'0']!=0) return false;
                        else cnt[item-'0']++;
                    }
                }
            }
            return true;
        }
        
        bool isValidBox(vector<vector<char> >& board){
            for(int i = 0 ; i < 3; ++ i){
                for(int j = 0 ; j < 3; ++ j){
                    vector<int> cnt(10,0);
                    for(int row = 3*i;row < 3*i+3; ++row){
                        for(int col = 3*j; col < 3*j+3; ++col){
                            char item = board[row][col];
                            if(item != '.'){
                                if(cnt[item-'0']!=0) return false;
                                else cnt[item-'0']++;
                            }
                        }
                    }
                }
            }
            return true;
        }
    
        bool isValidSudoku(vector<vector<char> > &board) {
            return isValidRow(board)&&isValidCol(board)&&isValidBox(board);
        }
    };
  • 相关阅读:
    Web开发实用网站资源
    Web开发实用网站资源
    Ubuntu的LAMP与相关软件安装设置
    Ubuntu的LAMP与相关软件安装设置
    试除法求最小N个素数之二
    试除法求最小N个素数之二
    Python程序-输出1000以内素数
    Python程序-输出1000以内素数
    Ubuntu安装MPICH3集群计算环境
    Ubuntu安装MPICH3集群计算环境
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3830416.html
Copyright © 2011-2022 走看看