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. Flutter学习笔记(五)

    一.Container 是一个便利的Widget,它把通用的绘制.定位和Widget的大小结合了起来. Container会先用padding填充子Widget和border之间的空白,然后添加其他的 ...

  2. Codeforces 101628A - Arthur's Language

    101628A - Arthur's Language 思路:dp,状态转移见代码. 代码: #include<bits/stdc++.h> using namespace std; #d ...

  3. SQL脚本去重分组统计

    需求:首先有一张表记录学生姓名.科目和成绩,然后模拟插入几条数据,脚本如下: create table score ( Name ),--姓名 subject ),--科目 grade int--成绩 ...

  4. 错误代码0x00000001,好多软件连不了网,求助~(WIN7/win8/win9/win10)

    解决办法: 以管理员身份运行命令行,在弹出的窗口中运行如下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 让被阻止了的 ...

  5. WPF自定义控件的两种方式

    方法A: 第一步:My自定义控件:Control 第二步:针对  “My自定义控件” 类型,编写<style>或<模板>(UI的外观完全由用户自己定义) 第三步: 使用My自定 ...

  6. highcharts图表配置参数汇总

    一.chart的部分相关属性说明    renderTo: 'container',      //图表的页面显示容器(也就是要显示到的div) chart.events.addSeries:添加数列 ...

  7. consumer filter

    ProtocolFilterWrapper中buildInvokerChain方法把Filter链在一起,调用执行的时候,逐个执行filter,最后执行filter中的invoker. //Proto ...

  8. 有名管道mkfifo

    int mkfifo(const char *pathname, mode_t mode); int mknod(const char *pathname, mode_t mode, dev_t de ...

  9. window8服务器

    安装PHP集成环境:XAMPP cmd下查看端口号: 如果直接输入netstat -nao 报不是内部指令的处理方法: c:\WINDOWS\system32\netstat -nao 就可以了. w ...

  10. Microsoft Office相关开发组件

    安装office,直接引用COM控件 C#4提供对PIA引用的一种方式:链接(编译器只会将PIA中需要的部分直接嵌入到程序集中),变体(variant)被视为动态类型,以减少强制转换需要的开销: 不安 ...