-
[LeetCode] 1260. Shift 2D GridLeetCode 2021. 10. 25. 17:08728x90
https://leetcode.com/problems/shift-2d-grid/
Shift 2D Grid - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Shift 2D Grid - Easy


m*n 벡터가 주어졌을때, 이를 k번 오른쪽으로 한칸씩 움직였을떄 결과를 나타내는 문제다.
k번 오른쪽으로 한칸씩 움직인다는것은 오른쪽으로 k칸 움직이는 걸로 이해할 수 있다.
이 때, 열 (j) 값은 (j+k)%n 이 되고,
행 (i) 값은 (i + (j+k)/n)%m 값이 된다.
전체 풀이 코드
class Solution { public: vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) { vector<vector<int>> ret = grid; int temp,np[2] = {}, m=grid.size(), n=grid[0].size(); for (int i=0; i<m; i++) for (int j=0; j<n; j++) { np[0] = (i + (j+k)/n)%m; np[1] = (j + k)%n; ret[np[0]][np[1]] = grid[i][j]; } return ret; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 890. Find and Replace Pattern (0) 2021.10.26 [LeetCode] 226. Invert Binary Tree (0) 2021.10.26 [LeetCode] 1417. Reformat The String (0) 2021.10.25 [LeetCode] 1007. Minimum Domino Rotations For Equal Row (0) 2021.10.25 [LeetCode] 155. Min Stack (0) 2021.10.25