Cyclically Rotating a Grid LeetCode solution in Rust
1impl Solution {2pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {3let n = grid.len();45let m = grid[0].len();67let min = n.min(m) / 2;89let k = k as usize;1011let mut result = grid.clone();1213for pi in 0..min {14let mut temps = vec![];1516let x = pi;1718for y in pi..n - pi {19temps.push((y, x));20}2122let y = n - 1 - pi;2324for x in pi + 1..m - pi {25temps.push((y, x));26}2728let x = m - 1 - pi;2930for y in (pi..n - 1 - pi).rev() {31temps.push((y, x));32}3334let y = pi;3536for x in (pi + 1..m - 1 - pi).rev() {37temps.push((y, x));38}3940let len = temps.len();4142let ak = k % len;4344for i in 0..len {45let (ci, cj) = temps[i];4647let (ti, tj) = temps[(i + ak) % len];4849result[ti][tj] = grid[ci][cj];50}51}5253result54}55}