Welcome to Subscribe On Youtube
  • /**
    
     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:
     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.
     Example 2:
    
     Input:
     matrix = [
     [1,2],
     [2,2]
     ]
     Output: False
     Explanation:
     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].
    
     Follow up:
    
     What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
     What if the matrix is so large that you can only load up a partial row into the memory at once?
    
     */
    public class Toeplitz_Matrix {
    
        class Solution {
            public boolean isToeplitzMatrix(int[][] matrix) {
                for (int r = 0; r < matrix.length; ++r)
                    for (int c = 0; c < matrix[0].length; ++c)
                        if (r > 0 && c > 0 && matrix[r-1][c-1] != matrix[r][c])
                            return false;
                return true;
            }
        }
    }
    
  • // OJ: https://leetcode.com/problems/toeplitz-matrix/
    // Time: O(MN)
    // Space: O(1)
    class Solution {
    public:
        bool isToeplitzMatrix(vector<vector<int>>& matrix) {
            for (int i = 1; i < matrix.size(); ++i) {
                for (int j = 1; j < matrix[0].size(); ++j) {
                    if (matrix[i][j] != matrix[i - 1][j - 1]) return false;
                }
            }
            return true;
        }
    };
    
  • class Solution:
        def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
            m, n = len(matrix), len(matrix[0])
            return all(
                matrix[i][j] == matrix[i - 1][j - 1]
                for i in range(1, m)
                for j in range(1, n)
            )
    
    ############
    
    class Solution(object):
        def isToeplitzMatrix(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: bool
            """
            for row in xrange(len(matrix) - 1):
            	for col in xrange(len(matrix[0]) - 1):
            		if matrix[row][col] != matrix[row+1][col+1]:
            			return False
            return True
    
  • class Solution {
        public boolean isToeplitzMatrix(int[][] matrix) {
            int rows = matrix.length, columns = matrix[0].length;
            int row = rows - 1, column = 0;
            while (row > 0) {
                row--;
                int num = matrix[row][column];
                for (int i = row + 1, j = column + 1; i < rows && j < columns; i++, j++) {
                    int curNum = matrix[i][j];
                    if (curNum != num)
                        return false;
                }
            }
            while (column < columns) {
                int num = matrix[row][column];
                for (int i = row + 1, j = column + 1; i < rows && j < columns; i++, j++) {
                    int curNum = matrix[i][j];
                    if (curNum != num)
                        return false;
                }
                column++;
            }
            return true;
        }
    }
    
  • // OJ: https://leetcode.com/problems/toeplitz-matrix/
    // Time: O(MN)
    // Space: O(1)
    class Solution {
    public:
        bool isToeplitzMatrix(vector<vector<int>>& matrix) {
            for (int i = 1; i < matrix.size(); ++i) {
                for (int j = 1; j < matrix[0].size(); ++j) {
                    if (matrix[i][j] != matrix[i - 1][j - 1]) return false;
                }
            }
            return true;
        }
    };
    
  • class Solution:
        def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
            m, n = len(matrix), len(matrix[0])
            return all(
                matrix[i][j] == matrix[i - 1][j - 1]
                for i in range(1, m)
                for j in range(1, n)
            )
    
    ############
    
    class Solution(object):
        def isToeplitzMatrix(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: bool
            """
            for row in xrange(len(matrix) - 1):
            	for col in xrange(len(matrix[0]) - 1):
            		if matrix[row][col] != matrix[row+1][col+1]:
            			return False
            return True
    

All Problems

All Solutions