作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/toeplitz-matrix/description/

题目描述

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].

题目大意

如果一个矩阵中每条从左上到右下的线上的数值都相等,那么满足题目条件,否则不满足。求判断输入的每个矩阵是否满足。

解题方法

方法一:两两比较

这个题目如果按照题目意思解答,求出矩阵的所有对角线,情况很复杂。所以使用的是直接两两进行比较的方式。

判断每行元素的每个元素和其右下角的元素是否相等,如果不等就返回False;全部相等返回True.

class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
for row in xrange(len(matrix) - 1):
for col in xrange(len(matrix[0]) - 1):
if matrix[row][col] != matrix[row+1][col+1]:
return False
return True

方法二:切片相等

只要观察到第二行的后面部分 和 第一行的前面部分相等即可。使用切片和一个for循环就能解决问题。

class Solution(object):
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
return all(matrix[row+1][1:] == matrix[row][:-1] for row in range(len(matrix)-1))

方法三:判断每条对角线

二刷的时候没有想到更简单的方法,所以直接使用了题目说的,判断每条对角线的方式。把第一行和第一列作为起始,然后判断向右下的每条线上的元素是否和它相等。

class Solution:
def isToeplitzMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: bool
"""
M, N = len(matrix), len(matrix[0])
for row in range(M):
first = matrix[row][0]
j = 0
for i in range(row, M):
if 0 <= j < N:
if matrix[i][j] != first:
return False
j += 1
for col in range(N):
first = matrix[0][col]
i = 0
for j in range(col, N):
if 0 <= i < M:
if matrix[i][j] != first:
return False
i += 1
return True

日期

2018 年 1 月 22 日
2018 年 11 月 8 日 —— 项目进展缓慢

【LeetCode】766. Toeplitz Matrix 解题报告的更多相关文章

  1. LeetCode 766 Toeplitz Matrix 解题报告

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

  2. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  3. LeetCode - 766. Toeplitz Matrix

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

  4. LeetCode 867 Transpose Matrix 解题报告

    题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...

  5. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  6. 【LEETCODE】45、766. Toeplitz Matrix

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

  7. 766. Toeplitz Matrix - LeetCode

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

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. Generic recipe for data analysis with general linear model

    Generic recipe for data analysis with general linear model Courtesy of David Schneider State populat ...

  2. Dango之form校验组件

    目录 1.引入案例 2. form组件的功能 3. form组件的使用 3.1 自定义form校验类 3.2 校验数据 3.3 渲染页面 3.4 展示错误信息 3.5 自定义校验结果 3.6 form ...

  3. mysql-计算排名

    mysql计算排名,获取行号rowno 学生成绩表数据 SELECT * FROM table_score ORDER BY score DESC; 获取某个学生成绩排名并计算该学生和上一名学生成绩差 ...

  4. 什么是DDL,DML,DCL

    转载自  https://www.2cto.com/database/201610/555167.html DML.DDL.DCL区别 . 总体解释: DML(data manipulation la ...

  5. Levenshtein莱文斯坦算法在项目中的应用

    简介 根据维基百科的描述,在信息理论.语言学和计算机科学中,莱文斯坦距离是一个测量两个序列之间差异的字符串度量.非正式地,两个单词之间的莱文斯坦距离是将一个单词改变为另一个单词所需的最小单字符编辑次数 ...

  6. 学习java的第十七天

    一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...

  7. above, abrupt

    above 近义词: over, beyond, exceeding反义词: below, beneath, under, underneath 有从右往左写的文字,没有从下往上的.above-men ...

  8. apostrophe

    apostrophe 者,', 0x27, 十进制39,ASCII里的single quote (单引号) 也.one of the 'inverted commas'. 在书写上可以表示所有格.省略 ...

  9. 03-Collection用例管理及批量执行

    当我们对一个或多个系统中的很多用例进行维护时,首先想到的就是对用例进行分类管理,同时还希望对这批用例做回归测试 .在postman也提供了这样一个功能,就是Collection .通过这个Collec ...

  10. navicat突然连接不上远程linux服务器上的mysql

    我linux服务器上的mysql是docker安装的,突然有一天我的navicat连接不上服务器上的mysql,于是开始了下面一系列的修复 1.首先登录服务器上mysql,看是否能正常登录,我发现不能 ...