• [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.

    方法:不必把空格都填好,只需检测给出的已经填好的数是否在行中、列中、小区域中重复,so easy,并不是检测它是否能填满哦

    方法1:

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {  
            // Note: The Solution object is instantiated only once.   
            vector<int> row(9,0);
            vector<int> col(9,0);
            vector<int> tmp(9,0); 
            for(int i = 0; i < 9; i++)  
            {  
                row = tmp;
                col = tmp;
                for(int j = 0; j < 9; j++)  
                {  
                    if(board[i][j] != '.')  
                    {  
                        if(row[board[i][j]-'1'] > 0)return false;  
                        else row[board[i][j]-'1']++;  
                    }  
                    if(board[j][i] != '.')  
                    {  
                        if(col[board[j][i]-'1'] > 0)return false;  
                        else col[board[j][i]-'1']++;  
                    }  
                }  
            }  
    
            for(int i = 0; i < 9; i+=3)  
                for(int j = 0; j < 9; j+=3)  
                {  
                    row = tmp;
                    for(int a = 0; a < 3; a++)  
                        for(int b= 0; b < 3; b++)  
                            if(board[i+a][j+b] != '.')  
                            {  
                                if(row[board[i+a][j+b]-'1']>0)return false;  
                                else row[board[i+a][j+b]-'1']++;  
                            }  
                }  
                return true;  
        }  
    };

    方法2:

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(board.size()<9 || board[0].size()<9)
               return false;
               
        vector<bool> tmp(9,false);
            vector<vector<bool> > r(9,tmp);
            vector<vector<bool> > c(r);
        vector<vector<bool> > b(r);
            
            for(int i=0; i<9; i++)
               for(int j=0; j<9; j++)
               {
                   if(board[i][j]=='.')
                     continue;
                   int a = (i/3)*3 + j/3;
                   int number = board[i][j]-'1';
                   if(r[i][number] || c[j][number] || b[a][number])
                       return false;
                   r[i][number] = c[j][number] = b[a][number] = 1;
               }
            return true;
        }
    };

    方法3:

    class Solution {
    public:
        bool isValidSudoku(vector<vector<char> > &board) {
            int row,row2;
            int col,col2;
            int sqr,sqr2;
            int a,b,c;
    
            for(int i=0;i<9;i++){
                row=0;row2 = 0;
                col=0;col2 = 0;
                sqr = 0;sqr2 =0;
                for(int j=0;j<9;j++){
                   a = board[i][j] - '0';
                   b = board[j][i] -  '0';
                   c = board[3*(i%3)+j/3][3*(i/3)+j%3] - '0';
                   if(a>0) row2 ^= 1<< a;
                   if(b>0) col2 ^= 1<< b;
                   if(c>0) sqr2 ^= 1<< c;
    
                   if(row2 < row || col2<col || sqr2<sqr)
                       return false;
                   row = row2;
                   col = col2;
                   sqr = sqr2;
                }
            }
            return true;
        }
    };
  • 相关阅读:
    用户控件的缓存技术之二【共三篇】
    .NET获取URL的各种方式及其区别
    图片上传封装类【包括图片上传和缩略图上传】.NET
    .NET抓取数据范例 抓取页面上所有的链接
    JQuery基础 学习的一些例子以及手册
    呵呵呵呵。。。系统还原了,终于可以用IE登陆百度了
    不用框架使用ajax 纯js使用ajax post,get范例及其区别
    用ashx还是aspx写ajax响应
    repeater绑定数组、哈希表、字典 ArrayList/HashTable,Dictionary为datasource
    Access数据库访问类 帮助类
  • 原文地址:https://www.cnblogs.com/Xylophone/p/3936872.html
Copyright © 2020-2023  润新知