20240501 Finished

This commit is contained in:
jackfiled 2024-05-01 20:28:07 +08:00
parent 34b660e3a3
commit 7a201f1252
2 changed files with 83 additions and 1 deletions

View File

@ -118,3 +118,4 @@ mod p209_minimum_size_subarray_sum;
mod p30_substring_with_concatenation_of_all_words; mod p30_substring_with_concatenation_of_all_words;
mod p76_minimum_window_substring; mod p76_minimum_window_substring;
mod p36_valid_sudoku; mod p36_valid_sudoku;
mod p54_spiral_matrix;

View File

@ -0,0 +1,81 @@
/**
* [54] Spiral Matrix
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
let (height, width) = (matrix.len() as i32, matrix[0].len() as i32);
let mut result = Vec::with_capacity((height * width) as usize);
let mut matrix = matrix;
let directions = vec![(0, 1), (1, 0), (0, -1), (-1, 0)];
let mut pos: (i32, i32) = (0, 0);
let mut direction = 0;
loop {
result.push(matrix[pos.0 as usize][pos.1 as usize]);
matrix[pos.0 as usize][pos.1 as usize] = i32::MAX;
let mut flag = true;
let mut count = 0;
while flag {
if count > 4 {
break;
}
let next_pos = (
pos.0 + directions[direction].0,
pos.1 + directions[direction].1,
);
if next_pos.0 < 0 || next_pos.0 >= height || next_pos.1 < 0 || next_pos.1 >= width {
direction = (direction + 1) % 4;
count += 1;
continue;
} else {
if (matrix[next_pos.0 as usize][next_pos.1 as usize] == i32::MAX) {
direction = (direction + 1) % 4;
count += 1;
continue;
}
}
pos = next_pos;
flag = false;
}
if flag {
break;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_54() {
assert_eq!(
vec![1, 2, 3, 6, 9, 8, 7, 4, 5],
Solution::spiral_order(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]])
);
assert_eq!(
vec![1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7],
Solution::spiral_order(vec![
vec![1, 2, 3, 4],
vec![5, 6, 7, 8],
vec![9, 10, 11, 12]
])
);
}
}