Leetcode Valid Sudoku (C,c++,java,python)

Source: Internet
Author: User
Tags ord

Problem:

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.

The Sudoku board could be partially filled, where empty cells is 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: According to the rules of the Sudoku game, a 9-row 9-column chessboard, each row and each 3*3 of the small square can not have a duplicate number, and the number can only be 0-9, one by two to determine whether these rules are satisfied can
Topic: Given a two-dimensional array, to determine whether to meet the rules of the Sudoku game, do not need to judge the points are not filled, just to determine whether the existing numbers are satisfied
Java source Code (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 Language Source code (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 + + source code (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 source code (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 a range (9): Map=[0 for K in range (ten)] for J in Range (9): If board[i][j]== '. ': Continue if board[i][j]< ' 0 ' or board[i][j]> ' 9 ': Retu RN 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 (Ten)] 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]== '. ': Co                  Ntinue      num = Ord (board[k][l])-ord (' 0 ') if Map[num]==1:return False map[num]=1 Return True


Leetcode Valid Sudoku (C,c++,java,python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.