【LEETCODE】45、766. Toeplitz Matrix
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: IsToeplitzMatrix
* @Author: xiaof
* @Description: 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.
*
* Input:
* matrix = [
* [1,2,3,4],
* [5,1,2,3],
* [9,5,1,2]
* ]
* Output: True
* Explanation:
* In the above grid, the diagonals are:
* "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
* In each diagonal all elements are the same, so the answer is True.
*
* @Date: 2019/7/4 19:47
* @Version: 1.0
*/
public class IsToeplitzMatrix { public boolean solution(int[][] matrix) {
//就是比较斜线上是否是通一个数据
//每一斜线
//每次可以和下一行的斜线比较,这样依次比较
for(int i = 0; i < matrix.length - 1; ++i) {
for(int j = 0; j < matrix[i].length - 1; ++j) {
if(matrix[i][j] != matrix[i + 1][j + 1]) {
return false;
}
}
}
return true;
} public boolean isToeplitzMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length - 1; i++) {
for (int j = 0; j < matrix[i].length - 1; j++) {
if (matrix[i][j] != matrix[i + 1][j + 1]) return false;
}
}
return true;
} public static void main(String args[]) { int[][] matrix = {{1,2,3,4},{5,1,2,3},{9,5,1,2}}; IsToeplitzMatrix fuc = new IsToeplitzMatrix();
System.out.println(fuc.isToeplitzMatrix(matrix));
} }
【LEETCODE】45、766. Toeplitz Matrix的更多相关文章
- 【LEETCODE】48、867. Transpose Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
- 【LeetCode】2、Add Two Numbers
题目等级:Medium 题目描述: You are given two non-empty linked lists representing two non-negative integers. ...
随机推荐
- C++ 模板元编程 学习笔记
https://blog.csdn.net/K346K346/article/details/82748163 https://www.jianshu.com/p/b56d59f77d53 https ...
- 使用singer 转换gitbase 数据到postgresql
gitbase 是mysql server 的一个实现(主要是用来分析git仓库代码),但是里面好多功能可能并不是很强大(sql 的限制) 我们可以通过singer 的tap-mysql 将数据抽取到 ...
- [codewars] - int32 to IPv4 二进制十进制 ip地址转换
原题 https://www.codewars.com/kata/int32-to-ipv4/train/java Take the following IPv4 address: 128.32.10 ...
- Vue.set 向响应式对象中添加响应式属性,及设置数组元素触发视图更新
一.为什么需要使用Vue.set? vue中不能检测到数组和对象的两种变化: 1.数组长度的变化 vm.arr.length = 4 2.数组通过索引值修改内容 vm.arr[1] = ‘aa’ Vu ...
- ImageView.ScaleType
前言 对ImageView.ScaleType,学习安卓需掌握.以官方链接:http://android.xsoftlab.net/reference/android/widget/ImageView ...
- xmind 破解
邮箱:x@iroader 序列号: XAka34A2rVRYJ4XBIU35UZMUEEF64CMMIYZCK2FZZUQNODEKUHGJLFMSLIQMQUCUBXRENLK6NZL37JXP4P ...
- SSH框架整合2
===========================================web.xml================================================== ...
- Res-DenseNetSegmentation模型调试记录
参考:https://blog.csdn.net/AbstractSky/article/details/76769202 https://blog.csdn.net/jsliuqun/article ...
- SQLServer replace函数
declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='ssssfcfgghdghdfcccs' select repla ...
- [转]Java 之 Serializable 序列化和反序列化的概念,作用的通俗易懂的解释
原文地址:https://blog.csdn.net/qq_27093465/article/details/78544505 遇到这个 Java Serializable 序列化这个接口,我们可能会 ...