766. Toeplitz Matrix

Source: Internet
Author: User

A Matrix is Toeplitz if every diagonal from top-left to bottom-right have the same element.

Now given a 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:
In the above grid, the diagonals is:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements was the same, so the answer is True.

Example 2:

Input:
Matrix = [
[+],
[2,2]]
Output:false
Explanation:
The diagonal "[1, 2]" has different elements.

Note:

    1. Matrix would be a 2D array of integers.
    2. Matrix would has a number of rows and columns in range [1, 20].
    3. MATRIX[I][J] would be integers in range [0, 99].

Follow up:

    1. What if the matrix was stored on disk, and the memory is limited such so can only load at most one row of the matrix Into the memory at once?
    2. What if the matrix was so large, can only load up a partial row to the the memory at once?
class Solution:    def isToeplitzMatrix(self, matrix):        """        :type matrix: List[List[int]]        :rtype: bool        """        if len(matrix)==0:            return True        for i in range(1,len(matrix)):            for j in range(1,len(matrix[0])):                if matrix[i][j]!=matrix[i-1][j-1]:                    return False        return True

766. Toeplitz Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.