【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛
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:
- matrix will be a 2D array of integers.
- matrix will have a number of rows and columns in range [1, 20].
- 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 解题报告的更多相关文章
- LeetCode 766 Toeplitz Matrix 解题报告
题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- LeetCode - 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- LeetCode 867 Transpose Matrix 解题报告
题目要求 Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ov ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【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 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- KEGG通路图应该怎么看(转载)
转载:http://www.omicshare.com/forum/thread-107-1-3219.html (出处: OmicShare Forum) 不管是RNA-seq的分析数据,还是蛋白组 ...
- Docker镜像相关操作
批量导入镜像 ll *.tgz|awk '{print $NF}'|sed -r 's#(.*)#docker load -i \1#' |bash 批量打tag docker images | se ...
- c#年份筛选
年份: <script type="text/javascript" src="http://www.shicishu.com/down/WdatePicker.j ...
- Vue3 中有哪些值得深究的知识点?
众所周知,前端技术一直更新很快,这不 vue3 也问世这么久了,今天就来给大家分享下vue3中值得注意的知识点.喜欢的话建议收藏,点个关注! 1.createApp vue2 和 vue3 在创建实例 ...
- linux 实用指令时间日期类
linux 使用指令时间日期类 data 显示当前日期 基本语法 date 显示当前时间 date+%Y 显示当前年份 date+%m 显示当前月份 date+%d 显示当前是哪一天 date &qu ...
- ios加载html5 audio标签用js无法自动播放
html5 audio标签在ios 微信浏览器中是无法自动播放的,最近在做一个小的项目遇到这个问题,安卓和pc都是正常的,唯独ios不行,查阅了很多资料,找到了以下方法,也许不是最好用的方法,如果有更 ...
- Fragment以及懒加载
1.Fragments Fragment是Activity中用户界面的一个行为或者是一部分,你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activ ...
- jmeter设置参数化
设置参数化方法有3种 第一种: 1.打开 jmeter,导入badboy录制的脚本 导入后记得选择"step"右键选择change controller ->逻辑控制器-&g ...
- Spring标签库
spring提供了两个标签库文件:spring-form.tld(表单标签库,用于输出HTML表单) spring.tld(基础标签库,用于Spring数据绑定等) 使用步骤: 1,配置表单标签库, ...
- 小程序中使用less(最优方式)
写惯了less/sass,但是现在开发小程序缺还是css,很不习惯. 在网上搜的教程,要么是gulp,要么就是vscode的Easy-less的插件. 传统方式 我们来对比,这两种方式的优劣. Gul ...