题目描述:

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

要完成的函数:

bool isToeplitzMatrix(vector<vector<int>>& matrix)

说明:

1、这道题题意很清晰,给定一个矩阵,判断矩阵上的所有对角线,每一条对角线上的元素值是不是都相等,比如题目中给的例1,就是一个满足条件的矩阵。最后返回true或者false,表示矩阵满不满足条件。

2、笔者最开始觉得这道题又是比较麻烦的题目,又要设置行i列j的条件限制,然后一一比较元素值。但后来重新扫了一遍题目叙述,发现可以逐行地搬下来比较,没有被比较到的元素,也刚好就是不用比较的。

举个例子,第一行除了最后一个之外的其余元素,都搬下来与第二行的元素进行比较,而第二行第一个元素不会被比较到,也刚好就是不用比较的,只需要之后跟第三行比较。

所以,逻辑清晰,代码如下:

    bool isToeplitzMatrix(vector<vector<int>>& matrix)
{
int row=matrix.size(),col=matrix[0].size();
int i=0,j;
while(i<row-1)//这里是row-1,最后一行不用再比较
{
j=0;
while(j<col-1)//col-1,每行最后一个元素不用比较
{
if(matrix[i][j]!=matrix[i+1][j+1])
return false;
j++;
}
i++;
}
return true;
}

上述代码实测20ms,beats 77.39% of cpp submissions。

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 - 766. Toeplitz Matrix

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

  3. 766. Toeplitz Matrix - LeetCode

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

  4. 【LEETCODE】45、766. Toeplitz Matrix

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

  5. 【Leetcode_easy】766. Toeplitz Matrix

    problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...

  6. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

  7. [LeetCode&Python] Problem 766. Toeplitz Matrix

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

  8. 766. Toeplitz Matrix

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

  9. 766. Toeplitz Matrix斜对角矩阵

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

随机推荐

  1. Visual studio 2015/2017 opencv3.4 kineck2.0 osg realsense配置

    ----------------------------------------------包含目录-------------------------------------------------- ...

  2. CentOS搭建VSFTP服务器

    一.安装vsftpd 1.查看是否已经安装vsftpd 2.如果没有,就安装 3.测试是否安装成功 4.安装成功设置开机启动 二.配置vsftpd 1.修改配置文件/etc/vsftpd/vsftpd ...

  3. tp5 $this/Db

  4. bootstrap导入JavaScript插件

    Bootstrap的JavaScript插件可以单独导入到页面中,也可以一次性导入到页面中.因为在Bootstrap中的JavaScript插件都是依赖于jQuery库,所以不论是单独导入还一次性导入 ...

  5. xampp 添加ssl 访问

    编辑php.ini 文件,找到 “;extension=php_openssl.dll” (去掉前面的;号注释) <VirtualHost *:8090> DocumentRoot &qu ...

  6. UVa 11996 Jewel Magic (splay + Hash + 二分)

    题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令: 1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入2 p 删除第p个字符,后面的字符往前移3 p1 p2反转 ...

  7. CodeForces 690C2 Brain Network (medium)(树上DP)

    题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...

  8. Spring AOP 详解 【转】

      此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决.一方面是为了以更加灵活的方式来解决问题,另 ...

  9. CAD&CG GDC 2018大会论文录用名单

    Section 1 增强现实与图形学: 报告时间:2018-8-25 14:00-15:30 报告地点:会议室1 P000009 基于增强现实的产品质量信息传递方法 P000104 重彩画的风格转移 ...

  10. 编写高质量代码改善C#程序的157个建议——建议100:静态方法和实例方法没有区别

    建议100:静态方法和实例方法没有区别 静态方法在加载时机和内存使用上和实例方法完全一致.在这里,我们先引出一个概念“类型对象”.比如类型Person,我们都知道new Person() 会产生一个对 ...