Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions solution/1900-1999/1914.Cyclically Rotating a Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,64 @@ function rotateGrid(grid: number[][], k: number): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
66 changes: 65 additions & 1 deletion solution/1900-1999/1914.Cyclically Rotating a Grid/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ tags:

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -326,6 +332,64 @@ function rotateGrid(grid: number[][], k: number): number[][] {
}
```

#### Rust

```rust
impl Solution {
pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
53 changes: 53 additions & 0 deletions solution/1900-1999/1914.Cyclically Rotating a Grid/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
impl Solution {
pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
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
}
}
Loading