博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode:Toeplitz Matrix
阅读量:4315 次
发布时间:2019-06-06

本文共 1224 字,大约阅读时间需要 4 分钟。

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: TrueExplanation:123451239512In 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.Input: matrix = [[1,2],[2,2]]Output: FalseExplanation: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].

思路

这纯粹是一道找规律的题目,每一行的前n-1个数和下一行的后n-1个数相同

class Solution(object):    def isToeplitzMatrix(self, matrix):        """        :type matrix: List[List[int]]        :rtype: bool        """        flag = False        for key,value in enumerate(matrix):            if key + 1< len(matrix):                if value[:-1] == matrix[key+1][1:]:                    continue                else:                    flag = False                    break            else:                flag = True        return flag

转载于:https://www.cnblogs.com/xmxj0707/p/8455960.html

你可能感兴趣的文章
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
前后台验证字符串长度
查看>>
《算法导论 - 思考题》7-1 Hoare划分的正确性
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>