題目連結

想想看… 怎麼 one pass 進行檢查呢?

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

// handle special cases first
// [], "", ...
// range of input?
class Solution
{
public:
    bool isToeplitzMatrix(vector<vector<int>> &matrix)
    {
        int n = matrix.size();
        int m = matrix[0].size();
        for (int i = 1; i < n; i++)
            for (int j = 1; j < m; j++)
                if (matrix[i][j] != matrix[i - 1][j - 1])
                    return false;
        return true;
    }
};

// class Solution
// {
// private:
//     int n, m;
//     bool ok(int x, int y)
//     {
//         return (0 <= x && x < n) && (0 <= y && y < m);
//     }
//
// public:
//     bool isToeplitzMatrix(vector<vector<int>> &matrix)
//     {
//         n = matrix.size();
//         m = matrix[0].size();
//         for (int i = 0; i < n; i++) {
//             int x = i, y = 0;
//             while (ok(x, y)) {
//                 if (matrix[i][0] != matrix[x][y])
//                     return false;
//
//                 x++;
//                 y++;
//             }
//         }
//
//         for (int j = 0; j < m; j++) {
//             int x = 0, y = j;
//             while (ok(x, y)) {
//                 if (matrix[0][j] != matrix[x][y])
//                     return false;
//
//                 x++;
//                 y++;
//             }
//         }
//
//         return true;
//     }
// };

#ifdef LOCAL
int main()
{
    return 0;
}
#endif