-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagonal-traverse.cpp
135 lines (106 loc) · 3 KB
/
diagonal-traverse.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <vector>
using namespace std;
class Solution {
public:
// https://leetcode.com/problems/diagonal-traverse/
vector<int> findDiagonalOrder(vector<vector<int>> &matrix) {
//[
// [ 1, 2, 3 ],
// [ 4, 5, 6 ],
// [ 7, 8, 9 ]
//]
// => [1,2,4,7,5,3,6,8,9]
// Let M = the number of rows and N = the number of columns
//
// Address individual cells as matrix[m][n], 0 >= n,m < M
//
// The special case is where M == N, and the matrix is square.
const size_t M = matrix.size();
const size_t N = (M >= 1) ? matrix[0].size() : 0;
// What about when N != M ?
//
//[
// [ 1, 2, 3 ],
// [ 4, 5, 6 ],
//]
// => [1,2,4,5,3,6]
// r is always M*N elements long
vector<int> r(M * N, 0);
typedef enum {
UP,
DOWN,
} dir_t;
size_t m = 0;
size_t n = 0;
dir_t dir = UP;
for (size_t i = 0; i < r.size(); i++) {
r[i] = matrix[m][n];
if (false) {
} else if (UP == dir) {
// boundary conditions going up / right
if (0 == m) {
// we've hit the top of the matrix
// reverse direction
dir = DOWN;
if (n < N - 1) {
// we've hit the top of the matrix, but not the corner
// move to the next column but stay on the same row
n++;
} else {
// we've hit the top-right corner of the matrix
// move one row down but stay in the same column
m++;
}
} else {
// we have not hit the top of the matrix
if (n < N - 1) {
// regular update in up direction - no boundary conditions
m--;
n++;
} else {
// we've hit the right wall of the matrix
// move one row down but stay in the same column
// reverse direction
m++;
dir = DOWN;
}
}
} else if (DOWN == dir) {
// boundary conditions going down / left
if (0 == n) {
// we've hit the left of the matrix
// reverse direction
dir = UP;
if (m < M - 1) {
// we've hit the left of the matrix, but not the corner
// move to the next row but stay on the same column
m++;
} else {
// we've hit the bottom-left corner of the matrix
// move one column right but stay in the same row
n++;
}
} else {
// we have not hit the left wall of the matrix
if (m < M - 1) {
// regular update in down direction - no boundary conditions
m++;
n--;
} else {
// we've hit the bottom wall of the matrix
// move one column right but stay in the same row
// reverse direction
n++;
dir = UP;
}
}
}
}
return r;
}
};