From e67955f9acf61fdd7d6a5a194ca83c6e1643eda4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 2 May 2024 20:17:28 +0800 Subject: [PATCH] 20240502 Finished --- src/problem/mod.rs | 3 ++- src/problem/p48_rotate_image.rs | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/problem/p48_rotate_image.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index b358d9f..6576c5a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -118,4 +118,5 @@ mod p209_minimum_size_subarray_sum; mod p30_substring_with_concatenation_of_all_words; mod p76_minimum_window_substring; mod p36_valid_sudoku; -mod p54_spiral_matrix; \ No newline at end of file +mod p54_spiral_matrix; +mod p48_rotate_image; \ No newline at end of file diff --git a/src/problem/p48_rotate_image.rs b/src/problem/p48_rotate_image.rs new file mode 100644 index 0000000..a68ecd6 --- /dev/null +++ b/src/problem/p48_rotate_image.rs @@ -0,0 +1,42 @@ +/** + * [48] Rotate Image + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn rotate(matrix: &mut Vec>) { + let n = matrix.len(); + + // 首先上下水平翻转 + for i in (0..n / 2) { + for j in 0..n { + let t = matrix[i][j]; + matrix[i][j] = matrix[n - i - 1][j]; + matrix[n - i - 1][j] = t; + } + } + + // 对角线翻转 + for i in (0..n) { + for j in (0..i) { + let t = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = t; + } + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_48() { + } +}