题目:

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#版本的代码如下(关键步骤已经注释)

  1. public static bool IsToeplitzMatrix(int[,] matrix)
  2. {
  3. bool flag = true;
  4.  
  5. int row = matrix.GetLength(); //第一维的长度(即行数)
  6. int col = matrix.GetLength(); //第二维的长度(即列数)
  7.  
  8. for(int i=;i< row;i++)
  9. {
  10. for(int j=;j< col;j++)
  11. {
  12. //最后一列和最后一行不需要去判断
  13. if(j+!= col&& i+!= row)
  14. {
  15. //如果下一行下一列有不相等的就说明不满足条件
  16. if(matrix[i,j]!= matrix[i+, j+])
  17. {
  18. flag = false;
  19. break;
  20. }
  21. }
  22. }
  23. if(!flag)
  24. {
  25. break;
  26. }
  27. }
  28. return flag;
  29. }

Leetcode刷题C#版之Toeplitz Matrix的更多相关文章

  1. 【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 ...

  2. Leetcode刷题C#版之 Length of Last Word

    题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...

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

  4. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  5. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  6. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  7. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  8. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  9. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

随机推荐

  1. 从零开始学习前端JAVASCRIPT — 5、JavaScript基础BOM

    1:BOM(Browser  Object  Model)概念 window对象是BOM中所有对象的核心. 2:window属性(较少用) self:self代表自己,相当于window. windo ...

  2. html dl dt dd标签元素语法结构与使用

    dl dt dd认识及dl dt dd使用方法 标签用于定义列表类型标签. dl dt dd目录 dl dt dd介绍 结构语法 dl dt dd案例 dl dt dd总结 一.dl dt dd认识 ...

  3. 关于iconfont字体图标的使用

    今天用iconfont遇到了字体图标的使用问题.关于使用字体图标的方式在这里有介绍三种方式(css和js的引入和平时一样) 首先你要分清是用单个字体图标icon,还是多个字体图标icon 关于使用代码 ...

  4. 设置某个类使用或者禁用ARC

    设置这个类为ARC的 用:-fobjc-arc    设置这个类非ARC的:-fno-objc-arc 工程是非ARC的,但是引用的第三方类库是ARC的,所以要使用的时候,要单独设置这个第三方的类是A ...

  5. Java 中判断类和实例之间的关系

    判断类与实例的关系有以下三种方式 1.instanceof关键字,用来判断对象是否是类的实例 (对象 => 类 )   2.isAssignableFrom,用来判断类型间是否存在派生关系 (类 ...

  6. io调度策略noop的理解

    io电梯算法,网上一堆,在此不再赘述. 手上有几块厂商提供的sas的ssd,做如下实验. 考虑到没有磁头移动,ssd一般采用noop的io调度策略,结果看到如下的iostat测试数据: Device: ...

  7. Git-远程仓库的使用

    Git修改远程仓库地址 1.修改命令 git remote set-url origin [url] 例如:$ git remote set-url origin gitlab@gitlab.chum ...

  8. 通过Azure 存储账号URL鉴别是标准磁盘还是高性能磁盘

    对于不知道虚拟机磁盘是标准磁盘还是高性能磁盘时,我们可以通过nslookup解析存储账号的URL,来判断存储账号的类型,从而得知虚拟磁盘的类型 1.标准存储账号的解析结果,字母"st&quo ...

  9. junit4X系列--Assert与Hamcrest

    原文出处:http://www.blogjava.net/DLevin/archive/2012/05/12/377960.html.感谢作者无私分享 到目前,JUnit4所有的核心源码都已经讲解过了 ...

  10. WebSphere--部署Servlet

    在WebSphere应用服务器上部署 Servlet需要四个步骤:编译 Servlet 或 Web 应用程序.将类文件放到 WebSphere应用服务器上.将相关的 HTML.JSP 和 SHTML ...