-
Notifications
You must be signed in to change notification settings - Fork 10
/
matrix_diagonal_traverse.rs
184 lines (179 loc) · 5.04 KB
/
matrix_diagonal_traverse.rs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
(d=diagonal,i=row,j=col,len=cur_diagonal_len)
m=3,n=4:
d i j len
0 2 0 1
1 1 0 2
2 0 0 3
3 0 1 3
4 0 2 2
5 0 3 1
6 0 4 0
*/
#[test]
fn diagonal_traverse_from_bottom_left_in_bottom_right_direction() {
let (m, n) = (3, 4);
let m_minus_1 = m - 1;
// bottom_right_direction traverse
let mut mat = vec![vec![0; n]; m];
let mut order = 1;
for diagonal in 0..m + n {
// current diagonal start row and col
let row = m_minus_1.saturating_sub(diagonal);
let col = diagonal.saturating_sub(m_minus_1);
let cur_diagonal_len = (m - row).min(n - col);
println!("{} {} {} {}", diagonal, row, col, cur_diagonal_len);
for offset in 0..cur_diagonal_len {
// row+offset表示下方向 叠加 col+offset右方向
mat[row + offset][col + offset] = order;
order += 1;
}
}
// bottom_right_direction traverse
for row in mat.into_iter().take(m) {
println!("{:?}", row);
}
}
/// https://leetcode.com/problems/sort-the-matrix-diagonally/
fn diagonal_sort(mut mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let (m, n) = (mat.len(), mat[0].len());
let m_minus_1 = m - 1;
// bottom_right_direction traverse
for diagonal in 0..m + n {
// current diagonal start row and col
let row = m_minus_1.saturating_sub(diagonal);
let col = diagonal.saturating_sub(m_minus_1);
let cur_diagonal_len = (m - row).min(n - col);
let mut cur_diagonal = Vec::with_capacity(cur_diagonal_len);
println!("{} {} {} {}", diagonal, row, col, cur_diagonal_len);
for offset in 0..cur_diagonal_len {
cur_diagonal.push(mat[row + offset][col + offset]);
}
cur_diagonal.sort_unstable();
#[allow(clippy::manual_memcpy)]
for offset in 0..cur_diagonal_len {
mat[row + offset][col + offset] = cur_diagonal[offset];
}
}
mat
}
/// https://leetcode.com/problems/toeplitz-matrix/
fn is_toeplitz_matrix(mat: Vec<Vec<i32>>) -> bool {
let (m, n) = (mat.len(), mat[0].len());
let m_minus_1 = m - 1;
// bottom_right_direction traverse
for diagonal in 0..m + n {
// current diagonal start row and col
let row = m_minus_1.saturating_sub(diagonal);
let col = diagonal.saturating_sub(m_minus_1);
let cur_diagonal_len = (m - row).min(n - col);
for offset in 1..cur_diagonal_len {
if mat[row + offset][col + offset] != mat[row + offset - 1][col + offset - 1] {
return false;
}
}
}
true
}
/**
(d=diagonal,i=row,j=col,len=cur_diagonal_len)
m=3,n=3:
d i j len
0 0 0 1
1 0 1 2
2 0 2 3
3 1 2 2
4 2 2 1
*/
#[test]
fn diagonal_traverse_from_top_left_in_bottom_left_direction() {
let (m, n) = (3, 3);
let m_minus_1 = m - 1;
let mut mat = vec![vec![0; n]; m];
let mut order = 1;
for diagonal in 0..m + n {
let row = diagonal.saturating_sub(m_minus_1);
let col = if diagonal <= m_minus_1 {
diagonal
} else {
m_minus_1
};
let cur_diagonal_len = col + 1 - row;
println!(
"d={}: ({},{}), diagonal_len={}",
diagonal, row, col, cur_diagonal_len
);
for offset in 0..cur_diagonal_len {
mat[row + offset][col - offset] = order;
order += 1;
}
}
for row in mat {
println!("{:?}", row);
}
}
/// https://leetcode.com/problems/diagonal-traverse/
fn find_diagonal_order(mat: Vec<Vec<i32>>) -> Vec<i32> {
let m = mat.len();
let n = mat[0].len();
let mut ret = Vec::with_capacity(m * n);
for i in 0..m + n - 1 {
if i % 2 == 1 {
// direction: ↙️
let (x, y) = if i < n { (0, i) } else { (i - n + 1, n - 1) };
let (mut x, mut y) = (x, y as i32);
while x < m && y >= 0 {
ret.push(mat[x][y as usize]);
x += 1;
y -= 1;
}
} else {
// direction: ↗️
let (x, y) = if i < m { (i, 0) } else { (m - 1, i - m + 1) };
let (mut x, mut y) = (x as i32, y);
while x >= 0 && y < n {
ret.push(mat[x as usize][y]);
x -= 1;
y += 1;
}
}
}
ret
}
#[test]
fn test_find_diagonal_order() {
#[rustfmt::skip]
let test_cases = vec![
/* m=3, n=2
i=0, (0,0)
i=1, (0,1) (1,0)
i=2, (1,1) (2,0)
i=3, (2,1)
*/
(vec_vec![
[2, 5],
[8, 4],
[0, -1]
], vec![2, 5, 8, 0, 4, -1]),
/* m=3, n=3
i=0, (0,0)
i=2, (2,0)
i=4, (2,2)
*/
(
vec_vec![
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
vec![1, 2, 4, 7, 5, 3, 6, 8, 9],
),
(vec_vec![
[2, 3],
[2, 3]
], vec![2, 3, 2, 3]),
];
for (input, output) in test_cases {
assert_eq!(find_diagonal_order(input), output);
}
}