[leetcode]Valid Sudoku @ Python
原题地址:https://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 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.
解题思路:判断是否为合法的数独。
代码:
class Solution: # @param board, a 9x9 2D array # @return a boolean def isValidSudoku(self, board): def isValid(x, y, tmp): for i in range(9): if board[i][y]==tmp:return False for i in range(9): if board[x][i]==tmp:return False for i in range(3): for j in range(3): if board[(x/3)*3+i][(y/3)*3+j]==tmp: return False return True for i in range(9): for j in range(9): if board[i][j]=='.':continue tmp=board[i][j] board[i][j]='D' if isValid(i,j,tmp)==False: return False else: board[i][j]=tmp return True
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步