LeetCode 36 Valid Sudoku (C,C++,Java,Python)

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

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.

Solution:

根据数独游戏的规则,一个9行9列的棋盘,每行每列和每一个3*3的小方格都不能有重复的数字,并且数字只能是0-9,逐一判断这些规则是否满足即可

题目大意:

给定一个二维数组,判定是否满足数独游戏的规则,不需要判断没有填上的点,只需要判断已经有的数字是否满足

Java源代码(320ms):

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        int i,j,k,l;
        if(board.length!=9 || board[0].length!=9)return false;
        int[] map;
        for(i=0;i<9;i++){
            map= new int[10];
            for(j=0;j<9;j++){
                if(board[i][j]=='.')continue;
                if(board[i][j]<'0' || board[i][j]>'9')return false;
                int num=board[i][j]-'0';
                if(map[num]==1)return false;
                map[num]=1;
            }
        }
        for(j=0;j<9;j++){
            map=new int[10];
            for(i=0;i<9;i++){
                if(board[i][j]=='.')continue;
                int num=board[i][j]-'0';
                if(map[num]==1)return false;
                map[num]=1;
            }
        }
        for(i=0;i<9;i+=3){
            for(j=0;j<9;j+=3){
                map=new int[10];
                for(k=i;k<i+3;k++){
                    for(l=j;l<j+3;l++){
                        if(board[k][l]=='.')continue;
                        int num=board[k][l]-'0';
                        if(map[num]==1)return false;
                        map[num]=1;
                    }
                }
            }
        }
        return true;
    }
}

C语言源代码(4ms):

bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
    int map[10],i,j,k,l,num;
    if(boardRowSize!=9 || boardColSize!=9)return false;
    for(i=0;i<9;i++){
        memset(map,0,sizeof(map));
        for(j=0;j<9;j++){
            if(board[i][j]=='.')continue;
            if(board[i][j]<'0' || board[i][j]>'9')return false;
            num=board[i][j]-'0';
            if(map[num]!=0)return false;
            map[num]=1;
        }
    }
    for(j=0;j<9;j++){
        memset(map,0,sizeof(map));
        for(i=0;i<9;i++){
            if(board[i][j]=='.')continue;
            num=board[i][j]-'0';
            if(map[num]!=0)return false;
            map[num]=1;
        }
    }
    for(i=0;i<9;i+=3){
        for(j=0;j<9;j+=3){
            memset(map,0,sizeof(map));
            for(k=i;k<i+3;k++){
                for(l=j;l<j+3;l++){
                    if(board[k][l]=='.')continue;
                    num=board[k][l]-'0';
                    if(map[num]!=0)return false;
                    map[num]=1;
                }
            }
        }
    }
    return true;
}

C++源代码(12ms):

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int i,j,k,l,map[10];
        if(board.size()!=9 || board[0].size()!=9)return false;
        for(i=0;i<9;i++){
            memset(map,0,sizeof(map));
            for(j=0;j<9;j++){
                if(board[i][j]=='.')continue;
                if(board[i][j]<'0' || board[i][j]>'9')return false;
                int num=board[i][j]-'0';
                if(map[num])return false;
                map[num]=1;
            }
        }
        for(j=0;j<9;j++){
            memset(map,0,sizeof(map));
            for(i=0;i<9;i++){
                if(board[i][j]=='.')continue;
                int num=board[i][j]-'0';
                if(map[num])return false;
                map[num]=1;
            }
        }
        for(i=0;i<9;i+=3){
            for(j=0;j<9;j+=3){
                memset(map,0,sizeof(map));
                for(k=i;k<i+3;k++){
                    for(l=j;l<j+3;l++){
                        if(board[k][l]=='.')continue;
                        int num=board[k][l]-'0';
                        if(map[num])return false;
                        map[num]=1;
                    }
                }
            }
        }
        return true;
    }
};

Python源代码(104ms):

class Solution:
    # @param {character[][]} board
    # @return {boolean}
    def isValidSudoku(self, board):
        if len(board)!=9 or len(board[0])!=9:return False
        for i in range(9):
            map=[0 for k in range(10)]
            for j in range(9):
                if board[i][j]=='.':continue
                if board[i][j]<'0' or board[i][j]>'9':return False
                num = ord(board[i][j])-ord('0')
                if map[num]==1:return False
                map[num]=1
        for j in range(9):
            map=[0 for k in range(10)]
            for i in range(9):
                if board[i][j]=='.':continue
                num = ord(board[i][j])-ord('0')
                if map[num]==1:return False
                map[num]=1
        for i in range(0,9,3):
            for j in range(0,9,3):
                map=[0 for k in range(10)]
                for k in range(i,i+3):
                    for l in range(j,j+3):
                        if board[k][l]=='.':continue
                        num = ord(board[k][l])-ord('0')
                        if map[num]==1:return False
                        map[num]=1
        return True
时间: 2024-10-15 07:48:23

LeetCode 36 Valid Sudoku (C,C++,Java,Python)的相关文章

leetCode 36. Valid Sudoku(数独) 哈希

36. 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 S

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析:

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

LeetCode 20 Valid Parentheses (C,C++,Java,Python)

Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "

LeetCode 36 Valid Sudoku(合法的数独)

题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入数字的数组值为’.’ 判断当前所给已知数组中所填的数字是否合法. 数独合法性判断: 1. 满足每一行的数字都只能是1~9,并且不能产生重复 2. 满足每一列的数字都只能是1~9,并且不能产生重复 3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复 判断过程: 初始化三个数组:row,

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/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 sud