数据结构算法操作试题(C++/Python)——有效的数独

文章目录


数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录


1. 题目

leetcode 链接:https://leetcode-cn.com/problems/valid-sudoku/submissions/
在这里插入图片描述
在这里插入图片描述

2. 解答

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 链接 评论区~