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:

  1. matrix will be a 2D array of integers.
  2. matrix will have a number of rows and columns in range [1, 20].
  3. 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的更多相关文章

  1. [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 ...

  2. 【Leetcode_easy】766. Toeplitz Matrix

    problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...

  3. 【LEETCODE】45、766. Toeplitz Matrix

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  4. 766. Toeplitz Matrix - LeetCode

    Question 766. Toeplitz Matrix Solution 题目大意: 矩阵从每条左上到右下对角线上的数都相等就返回true否则返回false 思路: 遍历每一行[i,j]与[i+1 ...

  5. LeetCode 766 Toeplitz Matrix 解题报告

    题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...

  6. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

  7. LeetCode - 766. Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

  8. [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 ...

  9. 766. Toeplitz Matrix

    A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...

随机推荐

  1. Unity 4.x 资源加载

    using UnityEngine; using System.Collections; using System.IO; public class LoadResource : MonoBehavi ...

  2. Longest Repeating Character Replacement

    2018-06-29 22:56:24 问题描述: 问题求解: 本题是一条字符串问题,且是求Optimal,自然最初想到的是使用DP来进行求解,但是问题就是如果采用DP的话,前一个状态也太多了,和替换 ...

  3. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  4. ArcGIS API for Windows Phone开发实例(4):点击查看超市信息 --- 关于使用InforWindow

    菩提老王的葡萄架:作品 地址:http://blog.newnaw.com/?p=696

  5. 在 Confluence 6 中的 Jira 高级权限

    启用嵌套用户组(Enable Nested Groups) 为嵌套组启用或禁用支持. 在启用嵌套用户组之前,你需要检查你在 JIRA 服务器中的嵌套用户组是否启用了.当嵌套用户组启用成功后,你可以将一 ...

  6. TitanX服务器登陆网关

  7. Leetcode 96

    class Solution { public: int numTrees(int n) { ]; dp[] = ; dp[] = ; dp[] = ; ;i <= n;i++){ ; ;j & ...

  8. entest1

      1◆ ai I   2◆ ai I ir ɜː ie i:   3◆ u: ʌ ɜː ə   ui u: ure ʊə

  9. linux目录管理、时钟管理、文件查看命令

    inux的两种时钟: 系统时钟:由Linux内核通过CPU的工作频率进行的计时: 硬件时钟: hwclock: 显示硬件时钟 -s, --hctosys -w, --systohc cal:日历 目录 ...

  10. struts1的parameter

    1.配置文件    parameter="method" 2.请求路径      http://localhost:8081/purchaseDeclareAction.do?me ...