1914. Cyclically Rotating a Grid Medium

@problem@discussion
#Array#Matrix#Simulation



1/**
2 * [1914] Cyclically Rotating a Grid
3 *
4 * You are given an m x n integer matrix grid​​​, where m and n are both even integers, and an integer k.
5 * 
6 * The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:
7 * 
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png" style="width: 231px; height: 258px;" />
9 * 
10 * A cyclic rotation of the matrix is done by cyclically rotating each layer in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the counter-clockwise direction. An example rotation is shown below:
11 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg" style="width: 500px; height: 268px;" />
12 * Return the matrix after applying k cyclic rotations to it.
13 * 
14 *  
15 * Example 1:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/rod2.png" style="width: 421px; height: 191px;" />
17 * 
18 * Input: grid = [[40,10],[30,20]], k = 1
19 * Output: [[10,20],[40,30]]
20 * Explanation: The figures above represent the grid at every state.
21 * 
22 * 
23 * Example 2:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid5.png" style="width: 231px; height: 262px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid6.png" style="width: 231px; height: 262px;" /> <img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid7.png" style="width: 231px; height: 262px;" />
25 * 
26 * 
27 * Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2
28 * Output: [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]
29 * Explanation: The figures above represent the grid at every state.
30 * 
31 * 
32 *  
33 * Constraints:
34 * 
35 * 
36 * 	m == grid.length
37 * 	n == grid[i].length
38 * 	2 <= m, n <= 50
39 * 	Both m and n are even integers.
40 * 	1 <= grid[i][j] <=^ 5000
41 * 	1 <= k <= 10^9
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/cyclically-rotating-a-grid/
47// discuss: https://leetcode.com/problems/cyclically-rotating-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn rotate_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
53        
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_1914() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.