【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 ...
随机推荐
- vim——批量缩进
批量缩进 第一种 按esc,退出编辑模式,到命令模式,并在英语输入法下输入":" 将所要批量缩进的行号写上,按照格式:"行号1,行号2>"输入命令,如要将 ...
- 59. Divide Two Integers
Divide Two Integers My Submissions QuestionEditorial Solution Total Accepted: 66073 Total Submission ...
- C语言中的main函数的参数解析
main()函数既可以是无参函数,也可以是有参的函数.对于有参的形式来说,就需要向其传递参数.但是其它任何函数均不能调用main()函数.当然也同样无法向main()函数传递,只能由程序之外传递而来. ...
- abort, about
abort 变变变: abortion:堕胎 abortionist:(非法)做堕胎手术的,不是所有的ist都是scientist, "All that glitters is not go ...
- Spark(八)【广播变量和累加器】
目录 一. 广播变量 使用 二. 累加器 使用 使用场景 自定义累加器 在spark程序中,当一个传递给Spark操作(例如map和reduce)的函数在远程节点上面运行时,Spark操作实际上操作的 ...
- 如何让Linux 机器CPU使用率变高
如何让Linux 机器CPU使用率变高 一.实现 1.单行命令搞定 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" ...
- iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法
iBatis会自动缓存每条查询语句的列名映射,对于动态查询字段或分页查询等queryForPage, queryForList,就可能产生"列名无效".rs.getObject(o ...
- Python实战之MySQL数据库操作
1. 要想使Python可以操作MySQL数据库,首先需要安装MySQL-python包,在CentOS上可以使用一下命令来安装 $ sudo yum install MySQL-python 2. ...
- mybatis中返回自动生成的id
当有时我们插入一条数据时,由于id很可能是自动生成的,如果我们想要返回这条刚插入的id怎么办呢. 在mysql数据中我们可以在insert下添加一个selectKey用以指定返回的类型和值: ...
- FindUserByPageServlet
package com.hopetesting.web.servlet;import com.hopetesting.domain.PageBean;import com.hopetesting.do ...