impl Solution {
pub fn longest_line(mat: Vec<Vec<i32>>) -> i32 {
let mut res = 0;
for (x, row) in mat.iter().enumerate() {
for (y, y_item) in row.iter().enumerate() {
if *y_item == 0 {continue;}
for (x_dir, y_dir) in vec!((0,1), (1,0), (1,1), (1, -1)) {
let mut cnt = 0;
let mut i = x as i32;
let mut j = y as i32;
while i >=0 && i < mat.len() as i32 && j >=0 && j < mat[0].len() as i32 && mat[i as usize][j as usize] == 1 {
i += x_dir;
j += y_dir;
cnt += 1;
}
res = std::cmp::max(cnt, res);
}
}
}
return res;
}
}
I thought Rust is preferring functional way for iterator, ex. to avoid ugly cast between i32 and usize indexing. In my case, I have to traversal not only vertical/horizonal but diagonal/anti-diagonal, Any functional way to tune code better looking ?