1260. Shift 2D Grid 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1123456789101112131415161718192021class Solution {public: vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) { vector<int> vec; int n =grid.size(), m=grid[0].size(); for(int i=0;i<n;++i){ for(int j = 0;j<m;++j){ vec.push_back(grid[i][j]); } } k%= m*n; k = m*n-k; for(int i=0;i<n;++i){ for(int j = 0;j<m;++j){ grid[i][j] = vec[k%(m*n)]; k++; } } return grid; }}; option 2 - O(1)analysis time complexity O(mn) space complexity O(nm) O(1)