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() && k < matrix[0].size();j ++,k ++)
11                 {
12                     if(matrix[j][k]!=init)
13                         result = false;
14                 }
15             }
16
17             for(int i = 1;i < matrix[0].size();i ++)
18             {
19                 int init = matrix[0][i];
20                 for(int j=1,k=i+1;j < matrix.size() && k < matrix[0].size();j ++,k ++)
21                 {
22                     if(matrix[j][k]!=init)
23                         result = false;
24                 }
25             }
26             return result;
27         }
28 };

原文地址:https://www.cnblogs.com/Asurudo/p/9771063.html

时间: 2024-08-30 18:31:14

Leetcode-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 59. Spiral Matrix II (螺旋矩阵之二)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题目标签:Array 这道题目和之前的螺旋矩阵几乎没有区别,而且更简单.同样按照螺旋矩阵的特性

[C++]LeetCode: 110 Spiral Matrix II (螺旋写入矩阵)

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:这道题和Spiral Matrix的模型和解法一样.要求我们将1~n^2的数字

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 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:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某