leetcode 题解 || Valid Sudoku 有关问题

leetcode 题解 || Valid Sudoku 问题

problem:

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 '.'.

leetcode 题解 || Valid Sudoku  有关问题

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.

Hide Tags
 Hash Table
判断一个给定的数独题目(未完全填充)是否有效

thinking:

(1)数独有效性判断的三个条件:1、每一行1~9不重复2、每一列1~9不重复3、每一块1~9不重复

(2)采用hash表判断,这里有一个小技巧: hash表初始化之后,没读取一个元素先进行判断,再修改hash表的值。

我第一次写的是先读取数独的元素修改hash表,最后再对hash表逐个判断,提交超时!!!

超时版本:

class Solution {
public:
    bool isValidSudoku(vector<vector<char> > &board) {
        bool flag_row=true,flag_column=true,flag_box=true;
        for(int row=0;row<9;row++)
        {
           if( !test_row(board,row))

                flag_row=false;
        }
        for(int column=0;column<9;column++)
        {
           if( !test_column(board,column))

                flag_column=false;
        }
        for(int i=0;i<9;i+3)
        {
            for(int j=0;j<9;j+3)
            {
                if(!test_box(board,i,j))
                    flag_box=false;
            }
        }
        return flag_box&&flag_column&&flag_row;
    }
protected:
    bool test_row(vector<vector<char> > &board, int row)
    {
        vector<int> array(9,0);
        for(int i=0;i<9;i++)
        {
            //int index=row*9+i;
            char a = board[row][i];
            if(a!='.')
                array[a-49]+=1;
        }
        for(vector<int>::const_iterator it=array.begin();it!=array.end();it++)
        {
            if(*it>1)
                return false;
        }
        return true;
    }

    bool test_column(vector<vector<char> > &board, int column)
    {
        vector<int> array(9,0);
        for(int i=0;i<9;i++)
        {
           //int index = i*9+index;
            char a = board[i][column];
            if(a!='.')
                array[a-49]+=1;
        }
        for(vector<int>::const_iterator it=array.begin();it!=array.end();it++)
        {
            if(*it>1)
                return false;
        }
        return true;
    }

    bool test_box(vector<vector<char> > &board, int i, int j)
    {
        vector<int> array(9,0);
        int start = 3*i+27*j;
        for(int k=0;k<9;k++)
        {
           // int index = start+(k/3)*9;
            char a = board[i+k/3][j+k%3];
            if(a!='.')
                array[a-49]+=1;
        }
        for(vector<int>::const_iterator it=array.begin();it!=array.end();it++)
        {
            if(*it>1)
                return false;
        }
        return true;
    }
};

先判断后修改hash表的版本,简化版本:

class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
        // Note: The Solution object is instantiated only once.
		vector<vector<bool>> rows(9, vector<bool>(9,false));
		vector<vector<bool>> cols(9, vector<bool>(9,false));
		vector<vector<bool>> blocks(9, vector<bool>(9,false));

		for(int i = 0; i < 9; i++)
		{
			for(int j = 0; j < 9; j++)
			{
				if(board[i][j] == '.')continue;
				int num = board[i][j] - '1';
				if(rows[i][num] || cols[j][num] || blocks[i - i%3 + j/3][num])
					return false;
				rows[i][num] = cols[j][num] = blocks[i - i%3 + j/3][num] = true;
			}
		}
		return true;
    }
};

天壤之别啊!!!