766. Toeplitz Matrix Toeplitz矩阵

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:
1234
5123
9512

In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and 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].
  1. class Solution:
  2. def isToeplitzMatrix(self, matrix):
  3. """
  4. :type matrix: List[List[int]]
  5. :rtype: bool
  6. """
  7. if not matrix or not matrix[0]:
  8. return False
  9. rowNum = max(0, len(matrix) - 1)
  10. colNum = max(0, len(matrix[0]) - 1)
  11. if rowNum is 0 or colNum is 0:
  12. return True
  13. for i in range(rowNum):
  14. for j in range(colNum):
  15. if matrix[i][j] != matrix[i + 1][j + 1]:
  16. return False
  17. return True
  18. # matrix = [
  19. # [1, 2, 3, 4],
  20. # [5, 1, 2, 3],
  21. # [9, 5, 1, 2]
  22. # ]
  23. # matrix = [
  24. # [1, 2],
  25. # [2, 2]
  26. # ]
  27. matrix = [
  28. [39, 24],
  29. [24, 39],
  30. [65, 24]
  31. ]
  32. #matrix = [[10], [11], [12]]
  33. s = Solution()
  34. res = s.isToeplitzMatrix(matrix)
  35. print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8331680.html

时间: 2024-10-31 18:47:01

766. Toeplitz Matrix Toeplitz矩阵的相关文章

【Leetcode_easy】766. Toeplitz Matrix

problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { for(int i=0; i<matrix.size()-1; ++i) { for(int j=0; j<matrix[0].size()-1; ++j) { if(matrix[i][j] != matrix[i+1][j+1

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N: 矩阵C = A*B 矩阵M=CN?N 将矩阵M中的所有元素取模6,得到新矩阵M' 计算矩阵M'中所有元素的和 解题思路:因为矩阵C为N*N的矩阵,N最大为1000,就算用快速幂也超时,但是因为C = A*B, 所以CN?N=ABAB-AB=AC′N?N?1B,C' = B*A, 为K*K的矩阵,K最大为6,完全可以接受. #include <cstdio> #inc

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640 

HDU4920 Matrix multiplication 矩阵

不要问我 为什么过了 窝也不造为什么就过了 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include<iostream> #inclu

[CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. LeetCode中的原题,请参见我之前的博客Set Matrix Zeroes 矩阵赋零.

hdu 4920 Matrix multiplication(矩阵坑题)

http://acm.hdu.edu.cn/showproblem.php?pid=4920 被这道题虐了一下午,啥也不说了.继续矩阵吧. 超时就超在每步取余上,要放在最后取余,再者注意三个循环的次序. #include <stdio.h> #include <map> #include <set> #include <stack> #include <queue> #include <vector> #include <cma

css3 matrix 2D矩阵和canvas transform 2D矩阵

一看到“2D矩阵”这个高大上的名词,有的同学可能会有种畏惧感,“矩阵”,看起来好高深的样子,我还是看点简单的吧.其实本文就很简单,你只需要有一点点css3 transform的基础就好. 没有前戏,直奔主题 2D矩阵指的是元素在2D平面内发生诸如缩放.平移.旋转.拉伸四种变化,在css3中对应4个方法分别是scale().translate().rotate()和skew(),可以说这4个方法是css3矩阵matrix的快捷方式,因为这4个方法本质都是由matrix实现的.类似地,在canvas

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

POJ 3318 Matrix Multiplication(矩阵乘法)

题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. 1 //3318 2 #include <stdio.h> 3 #include <string.h> 4 #include <time.h> 5 #include <stdlib.h> 6 #include <iostream> 7 8 using nam