Skip to content

Instantly share code, notes, and snippets.

@tonymontaro
Created September 23, 2019 07:58
Show Gist options
  • Select an option

  • Save tonymontaro/995102c29e1da8afee99eb17a81a51b2 to your computer and use it in GitHub Desktop.

Select an option

Save tonymontaro/995102c29e1da8afee99eb17a81a51b2 to your computer and use it in GitHub Desktop.
Scampa's problem: Rotate Image (https://leetcode.com/problems/rotate-image/)
class Solution:
def rotate(self, matrix: list) -> None:
n = len(matrix)
for col in range(n//2):
for i in range(col, n-col-1):
startVal = matrix[col][i]
current = start = (col, i)
while True:
next_ = self.getNext(current, n)
if next_ == start:
matrix[current[0]][current[1]] = startVal
break
self.swap(current, next_, matrix)
current = next_
return matrix
def swap(self, first, second, m):
m[first[0]][first[1]], m[second[0]][second[1]] = m[second[0]][second[1]], m[first[0]][first[1]]
def getNext(self, current, n):
return (n-current[1]-1, current[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment