当前位置:网站首页>Leetcode simple question: check whether each row and column contain all integers
Leetcode simple question: check whether each row and column contain all integers
2022-07-05 22:02:00 【·Starry Sea】
subject
For a size of n x n In terms of the matrix of , If each row and column contains from 1 To n Of All Integers ( contain 1 and n), The matrix is considered to be a It works matrix .
Give you a size of n x n The integer matrix of matrix , Please judge whether the matrix is an effective matrix : If it is , return true ; otherwise , return false .
Example 1:
Input :matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output :true
explain : In this case ,n = 3 , Each row and column contains numbers 1、2、3 .
therefore , return true .
Example 2:
Input :matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output :false
explain : In this case ,n = 3 , But the first row and first column do not contain numbers 2 and 3 .
therefore , return false .
Tips :
n == matrix.length == matrix[i].length
1 <= n <= 100
1 <= matrix[i][j] <= n
source : Power button (LeetCode)
Their thinking
Just check whether there are repeated numbers in each row of the matrix , Whether there is a duplicate number in each column .
class Solution:
def checkValid(self, matrix: List[List[int]]) -> bool:
for i in matrix: # Check each line
if len(set(i))!=len(matrix):
return False
temp=set()
for i in range(len(matrix)): # Check each column
temp.clear()
for j in range(len(matrix)):
temp.add(matrix[j][i])
if len(temp)!=len(matrix):
return False
return True
边栏推荐
猜你喜欢
The American Championship is about to start. Are you ready?
装饰器学习01
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Recovery technology with checkpoints
Implementation technology of recovery
Concurrency control of performance tuning methodology
Official clarification statement of Jihu company
Talking about MySQL index
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
EBS Oracle 11g 克隆步骤(单节点)
随机推荐
Poj 3237 Tree (Tree Chain Split)
Shell script, awk uses if, for process control
Official clarification statement of Jihu company
"Chris Richardson microservices series" uses API gateway to build microservices
微服務鏈路風險分析
Four components of logger
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
阿龙的感悟
Codeforces 12D Ball 树形阵列模拟3排序元素
854. String BFS with similarity K
Huawei fast game failed to call the login interface, and returned error code -1
华为快游戏调用登录接口失败,返回错误码 -1
Evolution of large website architecture and knowledge system
Win11运行cmd提示“请求的操作需要提升”的解决方法
Analysis and test of ModbusRTU communication protocol
Talking about MySQL index
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
How to develop and introduce applet plug-ins
装饰器学习01
[Yugong series] go teaching course 003-ide installation and basic use in July 2022