Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48 | Rotate Image | GoLang #1071

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions GO/Rotate_Image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Link: https://leetcode.com/problems/rotate-image/
//
// 48. Rotate Image
//
// You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
//
// You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

func rotate(matrix [][]int) {

n := len(matrix)

for i := range matrix {
for j := i; j < n; j++ {
temp := matrix[i][j]
matrix[i][j] = matrix[j][i]
matrix[j][i] = temp
}
}

for i := range matrix {
for j := 0; j < (n / 2); j++ {
temp := matrix[i][j]
matrix[i][j] = matrix[i][n-j-1]
matrix[i][n-j-1] = temp
}
}
}