[LeetCode]#1260. Shift 2D Grid

  • 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].
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]
Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]
  1. Covert to matrix to list.
  2. Calculate how to slice based on input k.
  3. Combine slice result.
  4. Convert list to matrix based on moving result.
class Solution:
def shiftGrid(self, grid, k):
k = k % len(nums)
nums = nums[move:] + nums[:move]
for i in range(0,len(nums),col):
return ans




Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Build a GeoDjango Web App to Store and Query Locations

What happens when you type ls -l in the shell?

Deploy your JBang script to Kubernetes/OpenShift

Creational Design Pattern: Singleton

Monitoring the Weather Information

Defining and fine tuning an API in Laravel with the Stoplight Platform

Python and DynamoDB

What is a cloud landing zone

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

Pattern 5 : Cyclic Sort

LeetCode #3 | Longest Substring Without Repeating Characters (Python)

Leetcode Coding: Count Number of Ways to Place Houses

Non Decreasing Array Leetcode Py