Leetcode刷题C#版之Toeplitz Matrix
题目:
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].
拿到题目最容易的就是想到遍历数组的行和列,判断某行某列与该行+1和该列+1的值是否相等。C#版本的代码如下(关键步骤已经注释)
public static bool IsToeplitzMatrix(int[,] matrix)
{
bool flag = true; int row = matrix.GetLength(); //第一维的长度(即行数)
int col = matrix.GetLength(); //第二维的长度(即列数) for(int i=;i< row;i++)
{
for(int j=;j< col;j++)
{
//最后一列和最后一行不需要去判断
if(j+!= col&& i+!= row)
{
//如果下一行下一列有不相等的就说明不满足条件
if(matrix[i,j]!= matrix[i+, j+])
{
flag = false;
break;
}
}
}
if(!flag)
{
break;
}
}
return flag;
}
Leetcode刷题C#版之Toeplitz Matrix的更多相关文章
- 【LeetCode刷题Java版】Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- Leetcode刷题C#版之 Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Leetcode刷题C#版之 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- tomcat服务器一闪而过解决方法
JDK没有配置,下载JDK安装到电脑上,然后在电脑->属性->高级系统设置->环境变量,将JDK中bin文件的目录E:\Program Files (x86)\Java\jre7\b ...
- python+appium+unittest
一个流行语言,一个主流工具,一个实用框架: For android 实例如下: import unittest from appium import webdriver from time impor ...
- Robots.txt - 禁止爬虫
robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: value.常见的规则行:User-A ...
- iOracle实战笔记(第五天)
导读 今天的主要内容:维护数据的完整性.索引.管理Oracle的权限和角色. 一.维护数据库的数据的完整性 数据完整性用于确保数据库数据遵从一定的商业规则和逻辑规则.在Oracle中,数据完整性可以使 ...
- Powerdesigner+Execel
1.将Powerdesigner中的表(PDM)导入到execel中 Ctrl+Shift+X/tool->Execute commands ->Edit/Run script 粘贴如下v ...
- python_如何对字典进行排序?
案例: 某班英语成绩以字典的形式存储为: {'lili':78, 'jin':50, 'liming': 30, ......} 依据成绩高低,进行学生成绩排名 如何对字典排序? 方法1: #!/us ...
- java1.8--改进的接口
关于接口,每天的编码都在写,就不多说了.这里对比下接口,抽象类,类3者的关系: 1),接口是一种规范,就是告诉外界这个东东可以做什么. 2),抽象类是一种模板,就是告诉外界这个东西的一部分公共功能. ...
- Linux平台ORACLE INSTANT客户端安装
下载安装文件 先去ORACLE官方网站下载所需版本的Instant Client Package 和 Instant Client Package - SQL*Plus安装包,(千万注意版本) htt ...
- Using $this when not in object context in
错误信息:$this引用没有上下文 原因:在PHP5中,static声明的静态方法里不可以使用$this 需要使用self来引用当前类中的方法或是变量. 引用的方法里不可以带$this(示例代码中为g ...
- 第三方模块paramiko的使用
"Paramiko" is a combination of the Esperanto words for "paranoid" and "frie ...