【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. ...
随机推荐
- packr 方便的潜入静态资源文件到golang 二进制文件中
类似的工具以前有介绍过statik,今天使用的工具是packr 也是很方便的golang tools 安装 go get -u github.com/gobuffalo/packr/packr 或者我 ...
- 使用terraform v0.12 生成gitlab repo 创建部署tf 文件
以前写过一个使用模版引擎+ rest 接口的模式,生成tf 文件,v0.12 直接提供了方便的json 处理函数 我们可以直接结合http 以及templatefile providers 方便的 ...
- kafka 创建消费者报错
kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...
- ES6 数组方法 forEach map filter find every some reduce
1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...
- 【CSP模拟赛】Freda的迷宫(桥)
题目描述 Freda是一个迷宫爱好者,她利用业余时间建造了许多迷宫.每个迷宫都是由若干房间和走廊构成的,每条走廊都连接着两个不同的房间,两个房间之间最多只有一条走廊直接相连,走廊都是双向通过. 黄昏 ...
- 在安卓手机下按钮会悬浮在键盘上,怎么解决vue.js
data里面 screenHeight: window.innerHeight mounted里面 mounted () { var that = this var u = navigator.use ...
- Windows 安装R
下载 R 的安装包 双击 安装包 进行安装 安装完成 测试 修改 R 中的CRAN镜像 添加到 Windows 的环境变量中 测试
- Kubernetes kubectl 命令概述
kubectl用于运行Kubernetes集群命令的管理工具. 语法 kubectl [command] [TYPE] [NAME] [flags] command:指定要在一个或多个资源执行的操作 ...
- windows 10下启用docker的k8s集群
安装Docker Desktop后,由于国内无法下载到Kubernete的文件,在Docker Desktop设置里勾选启用Kubernete一直显示”Kubernete is starting”状态 ...
- Servlet相关的几种乱码
1. 页面中文显示乱码 原因: response中的内容会先输入到response缓冲区,然后再输入到传给浏览器,所以要将缓冲区和浏览器的编码都设置成utf-8 1)未使用jsp,而是在servlet ...