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.

    不理解数独的概念的人好亏。

    1.每行每列的数字是1~9,且不得重复

    2.空的填'.'

    3.每个九宫格也是数字1~9,且不得重复

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char>>& board) {
            char temp;
            map<char,bool> row;
            map<char,bool> column;
            map<char,bool> grid;
            for(int i=0;i<9;i++){
                //先检查每行
                for(int j=0;j<9;)
                {   
                    temp=board[i][j];
                    if('1'<=temp<='9'&&!row[temp]) {j++; row[temp]=true;}
                    else if(temp=='.') j++;
                    else return false;
                }
                row.clear();
    
            }
            //再检查每列
            for(int m=0;m<9;m++){
                for(int k=0;k<9;)
                {   
                    temp=board[k][m];
                    if('1'<=temp<='9'&&!column[temp]) {k++; column[temp]=true;}
                    else if(temp=='.') k++;
                    else return false;
                }
                column.clear();
            }
            //在检查每个九宫格
            int hang=0,lie=0;
            for(int l=0;l<9;l++){
                for(int q=0;q<9;q++){
                    hang=(l/3)*3+q/3;
                    lie=q%3+(l%3)*3;
                    temp=board[hang][lie];
                    if('1'<=temp<='9'&&!grid[temp]) {grid[temp]=true;continue;}
                    else if(temp=='.') continue;
                    else return false;
                }
                grid.clear();
                
            }
            return true;
            
        }
    };
  • 相关阅读:
    算法训练(大富翁)
    算法训练题(奖励最小)
    算法训练题
    乔布斯
    算法题(符合题意的6位数)
    算法题(八皇后)
    算法题(分小组)
    汉诺塔
    递归算法题(兔子)
    字符串-mask-每个元音包含偶数次的最长子字符串
  • 原文地址:https://www.cnblogs.com/LUO77/p/5037226.html
Copyright © 2011-2022 走看看