原题

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

Follow up:

What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
What if the matrix is so large that you can only load up a partial row into the memory at once?

解法1

先从左到右, 再从上到下遍历matrix的对角线, 将对角线的元素放入集合里, 检查集合的长度是否大于1.

代码

class Solution(object):def isToeplitzMatrix(self, matrix):""":type matrix: List[List[int]]:rtype: bool"""row, col = len(matrix), len(matrix[0])# check from left to rightfor y in range(col):diag  = set()x = 0diag.add(matrix[x][y])while x+1 < row and y+1 < col:x += 1y += 1diag.add(matrix[x][y])# checkif len(diag) > 1:return False# check from up to downfor x in range(1, row):diag = set()y = 0diag.add(matrix[x][y])while x+1 < row and y+1 < col:x += 1y += 1diag.add(matrix[x][y])# checkif len(diag) > 1:return Falsereturn True

解法2

直接遍历matrix, 如果发现某个元素与它的对角线元素不相等, 返回False.

代码

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

[leetcode] 766. Toeplitz Matrix @ python相关推荐

  1. LeetCode 766. Toeplitz Matrix

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

  2. Leetcode PHP题解--D26 766. Toeplitz Matrix

    2019独角兽企业重金招聘Python工程师标准>>> 766. Toeplitz Matrix 题目链接 766. Toeplitz Matrix 题目分析 拓普利兹矩阵,应该不用 ...

  3. 766. 托普利茨矩阵(javascript)766. Toeplitz Matrix

    给你一个 m x n 的矩阵 matrix .如果这个矩阵是托普利茨矩阵,返回 true :否则,返回 false . 如果矩阵上每一条由左上到右下的对角线上的元素都相同,那么这个矩阵是 托普利茨矩阵 ...

  4. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  5. LeetCode 其他部分 简单 Python实现

    #LeetCode 其他部分 简单 Python实现 ''' 位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量).示例 : 输入: 11 ...

  6. Toeplitz matrix 与 Circulant matrix

    之所以专门定义两个新的概念,在于它们特殊的形式,带来的特别的形式. 1. Toeplitz matrix 对角为常数: n×nn\times n 的矩阵 AA 是 Toepliz 矩阵当且仅当,对于 ...

  7. LeetCode 73. Set Matrix Zeroes

    LeetCode 73. Set Matrix Zeroes Solution1:我的答案 比较笨,算法时间复杂度是O(mn)O(mn)O(mn),占用额外空间是O(m+n)O(m+n)O(m+n) ...

  8. LeetCode: 868. Transpose Matrix

    LeetCode: 868. Transpose Matrix 题目描述 Given a matrix A, return the transpose of A. The transpose of a ...

  9. C#LeetCode刷题之#766-托普利茨矩阵(Toeplitz Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3748 访问. 如果一个矩阵的每一方向由左上到右下的对角线上具有相 ...

最新文章

  1. 手工计算深度学习模型中的参数数量
  2. tensowflow 训练 远程提交_一文说清楚Tensorflow分布式训练必备知识
  3. 菜鸟教程python位运算符_NumPy 位运算
  4. 禁止修改varchar到int|[运维规范]
  5. CSDN、博客园等6大技术博客平台的写作体验测评
  6. MDK编译生成bin文件
  7. qt qss设置字体大小_Qt编写自定义控件70-扁平化flatui
  8. 在服务器上安装anaconda遇到的问题总结
  9. Java8————Stream API
  10. linux备份还原系统程序
  11. 面试题——栈的压入、弹出顺序
  12. android获取服务器时间格式,Android 获取服务器与客户端时差的实例代码
  13. slect,poll,epoll区别
  14. 微软:确实存在另一枚 print spooler 0day,目前尚未修复
  15. pppoe路由桥混合模式_192.168.1.1路由器怎么设置和登陆
  16. 【已解决】ws2_32.dll丢失怎么办?ws2_32.dll调用失败如何修复
  17. http(S)系列之(五):android之HttpURLConnection源码解析(1)
  18. N-gram 特征提取
  19. h5页面跳转关注公众号
  20. UEFI开发与调试---edk2中的Package

热门文章

  1. C# fileSystemWatcher类的Changed事件触发多次解决办法
  2. 一键GHOST硬盘版安装使用教程
  3. ffmpeg视频转码记录
  4. 计算机音乐谱 青花瓷,周杰伦经典曲目—钢琴谱:青花瓷(四音轨版)——五线谱.pdf...
  5. dos命令界面输入java -version没显示结果
  6. python爬虫-彻底解决网页乱码
  7. node中使用模板引擎
  8. 概率论与数理统计之全概率公式
  9. [ 常用工具篇 ] windows安装phpStudy_v8.1_X64
  10. 用正则表达式实现的计算器