1260. Shift 2D Grid Easy

@problem@discussion
#Array#Matrix#Simulation



1/**
2 * [1260] Shift 2D Grid
3 *
4 * Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.
5 * In one shift operation:
6 * 
7 * 	Element at grid[i][j] moves to grid[i][j + 1].
8 * 	Element at grid[i][n - 1] moves to grid[i + 1][0].
9 * 	Element at grid[m - 1][n - 1] moves to grid[0][0].
10 * 
11 * Return the 2D grid after applying shift operation k times.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e1.png" style="width: 400px; height: 178px;" />
15 * Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
16 * Output: [[9,1,2],[3,4,5],[6,7,8]]
17 * 
18 * Example 2:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e2.png" style="width: 400px; height: 166px;" />
20 * Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
21 * Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
22 * 
23 * Example 3:
24 * 
25 * Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
26 * Output: [[1,2,3],[4,5,6],[7,8,9]]
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	m == grid.length
32 * 	n == grid[i].length
33 * 	1 <= m <= 50
34 * 	1 <= n <= 50
35 * 	-1000 <= grid[i][j] <= 1000
36 * 	0 <= k <= 100
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/shift-2d-grid/
42// discuss: https://leetcode.com/problems/shift-2d-grid/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn shift_grid(grid: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
48        vec![]
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_1260() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.