leetcode-766-Toeplitz Matrix(每一条对角线元素的比较)
题目描述:
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:
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]
.
要完成的函数:
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(每一条对角线元素的比较)的更多相关文章
- LeetCode 766 Toeplitz Matrix 解题报告
题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- LeetCode - 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- 766. Toeplitz Matrix - LeetCode
Question 766. Toeplitz Matrix Solution 题目大意: 矩阵从每条左上到右下对角线上的数都相等就返回true否则返回false 思路: 遍历每一行[i,j]与[i+1 ...
- 【LEETCODE】45、766. Toeplitz Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】766. Toeplitz Matrix
problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vec ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- [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 ...
- 766. Toeplitz Matrix
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- 766. Toeplitz Matrix斜对角矩阵
[抄题]: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
随机推荐
- 天云CloudStack 改进版
整体风格 创建区域
- 面向对象的JavaScript-007-Function.prototype.bind() 的4种作用
1. // Function.prototype.bind() 的作用 // 1.Creating a bound function this.x = 9; var module = { x: 81, ...
- DapperExtensions 使用教程
最近搭建一个框架,使用dapper来做数据库访问,数据是sql server2012,支持多个数据库.事务.orm.ado.net原生操作方式,非常方便. 使用dapper的原因网上有很多文章说明,这 ...
- OSG相机与视图
转自:http://blog.csdn.net/wang15061955806/article/details/51603083 相机与视图 osg::Camera类用来管理OSG中的模型—— ...
- 【转载】Redis Sentinel服务配置
转载地址:http://blog.csdn.net/vtopqx/article/details/49247285 redis官网文档:http://www.redis.cn/topics/senti ...
- HttpUploader2-queue版本
1.1 2016版本 目标: 1.大幅度优化JS代码,面向开发人员更加友好,逻辑清晰,代码简洁,便于阅读,定制开发,扩展,更加符合企业级应用需求. 2.大幅度使用并发技术来提高上传速度,与HttpUp ...
- ScreenCapturePro2 for Joomla_3.4.7-ckeditor4x
1.1. 与Joomla_3.4.7整合-ck4 示例下载:Joomla_3.4.7, 1.1.1. 添加screencapture文件夹 路径:/media/screencapture 1. ...
- iOS基础教程:在建好的项目中加入CoreData[转]
这几天在做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项目初期,没有包含进CoreData.于是就在已建好的项目中加入CoreData.由于第一 ...
- scala文件流操作,生成sparkpv,uv作业文件
package com.bjsxt.scalaspark.core.examples.pvAnduv import java.io.Fileimport java.text.SimpleDateFor ...
- mybatis所需pom文件内容以及配置文件
官方网站http://www.mybatis.org/mybatis-3/zh/index.htmlpom文件<?xml version="1.0" encoding=&qu ...