数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录
leetcode 链接:https://leetcode-cn.com/problems/valid-sudoku/submissions/
python: 272ms, 11.9mb
class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ for i in range(9): if not self.isvalidList(board[i]) : return False if not self.isvalidList([j[i] for j in board]) : return False indexList = [0, 3, 6] if i % 3 == 0: for j in indexList: numsList = [] numsList += board[i][j:j + 3] numsList += board[i + 1][j:j + 3] numsList += board[i + 2][j:j + 3] print numsList if not self.isvalidList(numsList) : return False return True def isvalidList(self, myList): tmpList = [] for i in myList: if i != "." and i in tmpList: return False else: tmpList.append(i) return True
其他方法看 leetcode 链接 评论区~