码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-05-19 00:53:17      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:c   c++   java   python   leetcode   

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


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

标签:c   c++   java   python   leetcode   

原文地址:http://blog.csdn.net/runningtortoises/article/details/45830627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!