Class Solution
java.lang.Object
g1901_2000.s1914_cyclically_rotating_a_grid.Solution
1914 - Cyclically Rotating a Grid.<p>Medium</p>
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, where <code>m</code> and <code>n</code> are both <strong>even</strong> integers, and an integer <code>k</code>.</p>
<p>The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:</p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png" alt="" /></p>
<p>A cyclic rotation of the matrix is done by cyclically rotating <strong>each layer</strong> in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the <strong>counter-clockwise</strong> direction. An example rotation is shown below:</p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg" alt="" /></p>
<p>Return <em>the matrix after applying</em> <code>k</code> <em>cyclic rotations to it</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/19/rod2.png" alt="" /></p>
<p><strong>Input:</strong> grid = [[40,10],[30,20]], k = 1</p>
<p><strong>Output:</strong> [[10,20],[40,30]]</p>
<p><strong>Explanation:</strong> The figures above represent the grid at every state.</p>
<p><strong>Example 2:</strong></p>
<p><strong><img src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid5.png" alt="" /></strong> <strong><img src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid6.png" alt="" /></strong> <strong><img src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid7.png" alt="" /></strong></p>
<p><strong>Input:</strong> grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2</p>
<p><strong>Output:</strong> [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]</p>
<p><strong>Explanation:</strong> The figures above represent the grid at every state.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>2 <= m, n <= 50</code></li>
<li>Both <code>m</code> and <code>n</code> are <strong>even</strong> integers.</li>
<li><code>1 <= grid[i][j] <= 5000</code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
rotateGrid
public int[][] rotateGrid(int[][] grid, int k)
-