lintcode 容易题:Valid Sudoku 判断数独是否合法

题目:

判断数独是否合法

请判定一个数独是否有效。该数独可能只填充了部分数字,其中缺少的数字用 . 表示。

样例

下列就是一个合法数独的样例。

注意 一个合法的数独(仅部分填充)并不一定是可解的。我们仅需使填充的空格有效即可。

说明

什么是 数独?

http://sudoku.com.au/TheRules.aspx

http://baike.baidu.com/subview/961/10842669.htm

解题:

感觉这很难到不知道如何进行,在这里看到,只需判断每行,每类,每个小3*3矩阵内的数字是1-9就是合法的,但是这里只填充部分数的,按照上面说,没有填充的数 认为是该位置是合法的,这里的程序,也是这样搞的,感觉这不对头,但是意思是说:只要部分填充的数独满足了数独的条件,一定能填充成数独。<正确与否,我不确定,但是程序就是这样写的,逆推的结论>

Java程序:

class Solution {
    /**
      * @param board: the board
        @return: wether the Sudoku is valid
      */
    public boolean isValidSudoku(char[][] board) {
        boolean[] visited = new boolean[9];
        //row
        for(int i=0;i<9;i++){
            Arrays.fill(visited,false); //填充visited数组中的每个元素都是false
            for(int j=0;j<9;j++){
                if(!process(visited,board[i][j]))
                    return false;
            }
        }

        //col
        for(int i=0;i<9;i++){
            Arrays.fill(visited,false);
            for(int j=0;j<9;j++)
                if(!process(visited,board[j][i]))
                    return false;
        }
        //sub matrix
        for(int i=0;i<9;i+=3)
            for(int j=0;j<9;j+=3){
                Arrays.fill(visited,false);
                for(int k = 0;k<9;k++)
                    if(!process(visited,board[i+ k/3][j + k%3]))
                        return false;
            }
        return true;
    }
    private boolean process(boolean[] visited,char dight){
        if(dight==‘.‘)
            return true;
        int num = dight - ‘0‘;
        if(num<1 || num>9 || visited[num-1])
            return false;
        visited[num-1] = true;
        return true;
    }
};

总耗时: 822 ms

Python程序:

class Solution:
    # @param board, a 9x9 2D array
    # @return a boolean
    def isValidSudoku(self, board):

        # sub matrix
        for i in range(0,9,3):
            for j in range(0,9,3):
                visited = [False]*9
                for k in range(3):
                    m = i + k
                    n = j + k
                    if(self.process(visited,board[m][n])==False):
                        return False 

        # row
        for i in range(9):
            visited = [False]*9
            for j in range(9):
                if(self.process(visited,board[i][j])==False):
                    return False

        # col
        for j in range(9):
            visited = [False]*9
            for i in range(9):
                if(self.process(visited,board[i][j])==False):
                    return False

        return True

    def process(self,visited,digit):
        if digit==‘.‘:
            return True
        num = int(digit)
        if(num<1 or num>9 or visited[num-1]==True):
            return False
        visited[num-1] = True
        return True

总耗时: 148 ms

在判断3*3矩阵时候,根据我的这样方法很简单的哦

还有就是直判断行和列是否满足条件也能AC

所以我把判断小矩阵是否1-9组成放在了最上面

时间: 2024-10-26 17:49:55

lintcode 容易题:Valid Sudoku 判断数独是否合法的相关文章

判断数独是否合法(LintCode)

判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用. 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填充的空格有效即可. 说明 什么是 数独? http://sudoku.com.au/TheRules.aspx http://baike.baidu.com/subview/961/10842669.htm 一开始认为会超时于是有了用空间换时间的想法于是出现如下代码.. 后来一想,应该不会超时把..

[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

leetcode第35题--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 (partial

[LettCode]49. 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

leetcode_36题——Valid Sudoku()

Valid Sudoku Total Accepted: 33447 Total Submissions: 122453My Submissions Question Solution 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 c

[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. 题意:判断矩阵是否是一个数独矩阵.所谓的数独矩阵就是9*9的矩阵,每一行一个

LintCode_389 判断数独是否合法

题目 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填充的空格有效即可. 说明 什么是 数独? http://sudoku.com.au/TheRules.aspx http://baike.baidu.com/subview/961/10842669.htm 样例 下列就是一个合法数独的样例. C++代码 1 void clear(int *s, int n) 2 { 3 for(int i

389. Valid Sudoku【LintCode java】

Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character .. A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be vali

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

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 boa