[LeetCode&Python] Problem 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
Example 1:
Input:
matrix = [
[1,2,3,4],
[5,1,2,3],
[9,5,1,2]
]
Output: True
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.
Example 2:
Input:
matrix = [
[1,2],
[2,2]
]
Output: False
Explanation:
The diagonal "[1, 2]" has different elements.
Note:
matrixwill be a 2D array of integers.matrixwill have a number of rows and columns in range[1, 20].matrix[i][j]will be integers in range[0, 99].
class Solution:
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
Flag=True
m=len(matrix)
n=len(matrix[0]) for i in range(m):
row=i
column=0
stan=matrix[i][0]
while row<m and column<n:
if matrix[row][column]!=stan:
Flag=False
break
row+=1
column+=1 if Flag:
for j in range(1,n):
column=j
row=0
stan=matrix[0][j]
while column<n and row<m:
if matrix[row][column]!=stan:
Flag=False
break
row+=1
column+=1 return Flag
[LeetCode&Python] Problem 766. Toeplitz Matrix的更多相关文章
- [LeetCode&Python] Problem 867. Transpose Matrix
Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...
- 【Leetcode_easy】766. Toeplitz Matrix
problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...
- 【LEETCODE】45、766. Toeplitz Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 766. Toeplitz Matrix - LeetCode
Question 766. Toeplitz Matrix Solution 题目大意: 矩阵从每条左上到右下对角线上的数都相等就返回true否则返回false 思路: 遍历每一行[i,j]与[i+1 ...
- LeetCode 766 Toeplitz Matrix 解题报告
题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- LeetCode - 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- [LeetCode&Python] Problem 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
随机推荐
- 《剑指offer》第三十二题(不分行从上往下打印二叉树)
// 面试题32(一):不分行从上往下打印二叉树 // 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印. #include <iostream> #include ...
- java日期操作 大全
先来一个: 取得指定月份的第一天与取得指定月份的最后一天 http://iamin.blogdriver.com/iamin/847990.html )); } ...
- PHP面向对象初中高级之由浅入深
php面向对象编程基本实践:(了解类,类到对象的实例化,构造和析构,对象的引用); 类的概念: 物以类聚,把具有相似特性的对象对垒到一个类中 类定义了这些相似对象拥有的相同的属性和方法 类是相似对象的 ...
- codeforces 559b//Equivalent Strings// Codeforces Round #313(Div. 1)
题意:定义了字符串的相等,问两串是否相等. 卡了时间,空间,不能新建字符串,否则会卡. #pragma comment(linker,"/STACK:1024000000,102400000 ...
- CentOS7.6 Install TensorFlow
1. install pip 1). yum -y install epel-release 2). yum install python-pip 3). pip install --upgra ...
- IOS7 UI设计的十大准则
陈子木 iOS7 的用户界面设计比以往更卓越,并为用户提供了更具吸引力的独特体验,带来更大的机遇.在正式写代码之前,认真考虑UI设计是否符合这十条设计准则,可以提高App的可用性与吸引力.如果要更深入 ...
- RpcContext
RpcContext内部有一个ThreadLocal变量,它是作为ThreadLocalMap的key,表明每个线程有一个RpcContext. public class RpcContext { p ...
- top k
def top_k(arr, left, right, k): if left >= right: return pivot = arr[right] index = left for i in ...
- 信号处理signal、sigaction、pause、信号嵌套处理、不可重入函数
信号的捕捉和处理 主要由signal和sigaction函数来完成.还有一个函数pause,它可用来响应任何信号,不过不做任何处理. 1.signal函数 typedef void (*sighand ...
- @SpringBootApplication的使用
之前用户使用的是3个注解注解他们的main类.分别是@Configuration,@EnableAutoConfiguration,@ComponentScan.由于这些注解一般都是一起使用,spri ...