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

时间:2022-07-24
本文章向大家介绍数据结构算法操作试题(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 链接 评论区~