From 3433dd8f1baa785158b29db4faaa67fa1b517626 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 9 May 2026 08:05:11 +0800 Subject: [PATCH] feat: add solutions for lc No.1914 --- .../1914.Cyclically Rotating a Grid/README.md | 58 ++++++++++++++++ .../README_EN.md | 66 ++++++++++++++++++- .../Solution.rs | 53 +++++++++++++++ 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.rs diff --git a/solution/1900-1999/1914.Cyclically Rotating a Grid/README.md b/solution/1900-1999/1914.Cyclically Rotating a Grid/README.md index 803bfc4f7102b..25b6dab8d5533 100644 --- a/solution/1900-1999/1914.Cyclically Rotating a Grid/README.md +++ b/solution/1900-1999/1914.Cyclically Rotating a Grid/README.md @@ -309,6 +309,64 @@ function rotateGrid(grid: number[][], k: number): number[][] { } ``` +#### Rust + +```rust +impl Solution { + pub fn rotate_grid(grid: Vec>, k: i32) -> Vec> { + let mut grid = grid; + let m = grid.len(); + let n = grid[0].len(); + + let mut rotate = |p: usize, mut k: usize| { + let mut nums = Vec::new(); + for j in p..n - p - 1 { + nums.push(grid[p][j]); + } + for i in p..m - p - 1 { + nums.push(grid[i][n - p - 1]); + } + for j in (p + 1..n - p).rev() { + nums.push(grid[m - p - 1][j]); + } + for i in (p + 1..m - p).rev() { + nums.push(grid[i][p]); + } + let l = nums.len(); + if l == 0 { + return; + } + k %= l; + if k == 0 { + return; + } + for j in p..n - p - 1 { + grid[p][j] = nums[k]; + k = (k + 1) % l; + } + for i in p..m - p - 1 { + grid[i][n - p - 1] = nums[k]; + k = (k + 1) % l; + } + for j in (p + 1..n - p).rev() { + grid[m - p - 1][j] = nums[k]; + k = (k + 1) % l; + } + for i in (p + 1..m - p).rev() { + grid[i][p] = nums[k]; + k = (k + 1) % l; + } + }; + + let layers = std::cmp::min(m / 2, n / 2); + for i in 0..layers { + rotate(i, k as usize); + } + grid + } +} +``` + diff --git a/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md b/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md index 2b5a5d14a34c7..453eb391a48ad 100644 --- a/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md +++ b/solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md @@ -88,7 +88,13 @@ tags: -### Solution 1 +### Solution 1: Layer-by-Layer Simulation + +First, we compute the number of layers in the matrix, denoted by $p$, and then simulate the cyclic rotation layer by layer from the outside to the inside. + +For each layer, we traverse clockwise and append the elements on the top, right, bottom, and left edges to an array $nums$ in order. Let the length of $nums$ be $l$. Next, we take $k \bmod l$. Then, starting from index $k$ in the array, we write the elements back to the matrix along the top, right, bottom, and left edges in order. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. @@ -326,6 +332,64 @@ function rotateGrid(grid: number[][], k: number): number[][] { } ``` +#### Rust + +```rust +impl Solution { + pub fn rotate_grid(grid: Vec>, k: i32) -> Vec> { + let mut grid = grid; + let m = grid.len(); + let n = grid[0].len(); + + let mut rotate = |p: usize, mut k: usize| { + let mut nums = Vec::new(); + for j in p..n - p - 1 { + nums.push(grid[p][j]); + } + for i in p..m - p - 1 { + nums.push(grid[i][n - p - 1]); + } + for j in (p + 1..n - p).rev() { + nums.push(grid[m - p - 1][j]); + } + for i in (p + 1..m - p).rev() { + nums.push(grid[i][p]); + } + let l = nums.len(); + if l == 0 { + return; + } + k %= l; + if k == 0 { + return; + } + for j in p..n - p - 1 { + grid[p][j] = nums[k]; + k = (k + 1) % l; + } + for i in p..m - p - 1 { + grid[i][n - p - 1] = nums[k]; + k = (k + 1) % l; + } + for j in (p + 1..n - p).rev() { + grid[m - p - 1][j] = nums[k]; + k = (k + 1) % l; + } + for i in (p + 1..m - p).rev() { + grid[i][p] = nums[k]; + k = (k + 1) % l; + } + }; + + let layers = std::cmp::min(m / 2, n / 2); + for i in 0..layers { + rotate(i, k as usize); + } + grid + } +} +``` + diff --git a/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.rs b/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.rs new file mode 100644 index 0000000000000..99fb1a81d4471 --- /dev/null +++ b/solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.rs @@ -0,0 +1,53 @@ +impl Solution { + pub fn rotate_grid(grid: Vec>, k: i32) -> Vec> { + let mut grid = grid; + let m = grid.len(); + let n = grid[0].len(); + + let mut rotate = |p: usize, mut k: usize| { + let mut nums = Vec::new(); + for j in p..n - p - 1 { + nums.push(grid[p][j]); + } + for i in p..m - p - 1 { + nums.push(grid[i][n - p - 1]); + } + for j in (p + 1..n - p).rev() { + nums.push(grid[m - p - 1][j]); + } + for i in (p + 1..m - p).rev() { + nums.push(grid[i][p]); + } + let l = nums.len(); + if l == 0 { + return; + } + k %= l; + if k == 0 { + return; + } + for j in p..n - p - 1 { + grid[p][j] = nums[k]; + k = (k + 1) % l; + } + for i in p..m - p - 1 { + grid[i][n - p - 1] = nums[k]; + k = (k + 1) % l; + } + for j in (p + 1..n - p).rev() { + grid[m - p - 1][j] = nums[k]; + k = (k + 1) % l; + } + for i in (p + 1..m - p).rev() { + grid[i][p] = nums[k]; + k = (k + 1) % l; + } + }; + + let layers = std::cmp::min(m / 2, n / 2); + for i in 0..layers { + rotate(i, k as usize); + } + grid + } +}