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:

  1. matrix will be a 2D array of integers.
  2. matrix will have a number of rows and columns in range [1, 20].
  3. matrix[i][j] will be integers in range [0, 99].

如果数组中每一个对角线上的元素都相等,则返回true

C++(19ms):

 1 class Solution {
 2 public:
 3     bool isToeplitzMatrix(vector<vector<int>>& matrix) {
 4         int m = matrix.size() ;
 5         int n = matrix[0].size() ;
 6         for(int i = 0 ; i < m-1 ; i++){
 7             for(int j = 0 ; j < n-1 ; j++){
 8                 if (matrix[i][j] != matrix[i+1][j+1])
 9                     return false ;
10             }
11         }
12         return true ;
13     }
14 };

原文地址:https://www.cnblogs.com/mengchunchen/p/8327580.html

时间: 2024-11-01 08:45:46

766. Toeplitz Matrix的相关文章

【Leetcode_easy】766. Toeplitz Matrix

problem 766. Toeplitz Matrix solution1: class Solution { public: bool isToeplitzMatrix(vector<vector<int>>& matrix) { for(int i=0; i<matrix.size()-1; ++i) { for(int j=0; j<matrix[0].size()-1; ++j) { if(matrix[i][j] != matrix[i+1][j+1

766. Toeplitz Matrix Toeplitz矩阵

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 Explanati

Leetcode: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: 1234 51

Toeplitz Matrix——leetcode

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 Explanati

[LeetCode] 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 Explanati

Toeplitz Matrix - LeetCode

LeetCode 题目地址 描述 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]] Outpu

Leetcode-766 Toeplitz Matrix(托普利茨矩阵)

1 class Solution 2 { 3 public: 4 bool isToeplitzMatrix(vector<vector<int>>& matrix) 5 { 6 bool result = true; 7 for(int i = matrix.size()-1;i >= 0;i --) 8 { 9 int init = matrix[i][0]; 10 for(int j = i+1,k = 1;j < matrix.size() &&

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

离散时间信号与系统:4

12.系统 系统就是变换(Transformations,在线性代数中表示运动的一种描述) 将一个离散时域信号x映射到一个离散时域信号y.例如:磁共振成像系统. 其中,offset翻译成:补偿系统么? Decimate翻译成抽样系统么? 总结: 系统通过对信息的操作将一个信号变换为(transform)另一个信号. 只考虑: 将无限信号x变换为无限信号y. 将长度为N的信号x变换为长度为N的信号y. 12. 线性系统 两个性质: 缩放性和可加性. 注意:两个性质都是系统输出y随着系统输入x的变化