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-12-26 01:07:29