---
# System prepended metadata

title: Question 766. Toeplitz Matrix
tags: [leetcode]

---

###### tags: `leetcode`
# Question 766. Toeplitz Matrix
### Description:
Given an m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
### Solution:
Go through every diagonal line by checking edge point.
_O(m*n)_
### AC code
```cpp=
class Solution {
public:
    bool isToeplitzMatrix(vector<vector<int>>& matrix) {
        if(matrix.size()==1){
            return true;
        }
        int col = matrix[0].size();
        int row = matrix.size();
        int base, j, cur_col, cur_row;
        for(int i = 0 ; i < row ; i++){
            cur_row = i;
            cur_col = 0;
            base = matrix[cur_row][cur_col];
            if(cur_col+1 < col)
                cur_col++;
            else{
                continue;
            }
            for(cur_col ; cur_col < col ; cur_col++){
                if(cur_row+1 < row){
                    cur_row++;
                }
                else{
                    break;
                }
                if(base!=matrix[cur_row][cur_col])
                    return false;
            }
        }
        for(int i = 0 ; i < col ; i++){
            cur_col = i;
            cur_row = 0;
            base = matrix[cur_row][cur_col];
            if(cur_row+1 < row)
                cur_row++;
            else
                continue;
            for(cur_row ; cur_row < row ; cur_row++){
                if(cur_col+1 < col)
                    cur_col++;
                else
                    break;
                if(base!=matrix[cur_row][cur_col])
                    return false;
            }
        }
        return true;
    }
};
```
### AC code
```cpp=
class Solution {
public:
    string addStrings(string num1, string num2) {
        int len1 = num1.length()-1;
        int len2 = num2.length()-1;
        int remain = 0; int sum = 0;
        string ret = "";
        while(len1 >= 0 || len2 >= 0 || remain){
            sum = 0;
            if(len1 >= 0){
                sum += num1[len1]-'0';
                len1--;
            }
            if(len2 >= 0){
                sum += num2[len2]-'0';
                len2--;
            }
            sum += remain;
            //cout<<"sum: "<<sum<<endl;
            remain = sum/10;
            sum = sum%10;
            ret = to_string(sum) + ret;
        }
        return ret;
    }
};
```