# 1260. Shift 2D Grid ###### tags: `leetcode`,`easy` >ref: https://leetcode.com/problems/shift-2d-grid/ > Given a 2D grid of size m x n and an integer k. You need to shift the grid k times. In one shift operation: Element at grid[i][j] moves to grid[i][j + 1]. Element at grid[i][n - 1] moves to grid[i + 1][0]. Element at grid[m - 1][n - 1] moves to grid[0][0]. Return the 2D grid after applying shift operation k times. ![](https://i.imgur.com/dvwSjoy.png) ![](https://i.imgur.com/z5SDEX6.png) >Constraints: m == grid.length n == grid[i].length 1 <= m <= 50 1 <= n <= 50 -1000 <= grid[i][j] <= 1000 0 <= k <= 100 >1. mod(m\*n) list收集n元素後 用新的list收集n元素 直到收集m\*n個元素 >2. time O(n) spatial O(n) ```java= public List<List<Integer>> shiftGrid(int[][] grid, int k) { int m=grid.length; int n=grid[0].length; k=(m*n)- k%(m*n); List<List<Integer>> res= new LinkedList<>(); int row_count=0; List<Integer> row= new LinkedList<>(); for(int a=0 ; a < m*n ; a++){ k%=(m*n); int i= k/n; int j= k%n; k++; row.add(grid[i][j]); if(row_count++ == n-1){ res.add(row); row= new LinkedList<>(); row_count=0; } } return res; } ```