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.
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; } }
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; }
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; } };
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)
原文地址:http://blog.csdn.net/runningtortoises/article/details/45830627