2133. Check if Every Row and Column Contains All Numbers
An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.
Example 1:

Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.
Example 2:

Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.
Constraints:
n == matrix.length == matrix[i].length1 <= n <= 1001 <= matrix[i][j] <= n
創造2*n的集合 來儲存行跟列
如果已經發生 return False
否則+進去集合裡面
就這樣
class Solution: def checkValid(self, matrix: List[List[int]]) -> bool: n=len(matrix) L=[set() for i in range(2*n)] for i in range(n): for j in range(n): if matrix[i][j] in L[i] or matrix[i][j] in L[j+n]: return False else: L[i].add(matrix[i][j]) L[j+n].add(matrix[i][j]) return True
留言
張貼留言