From ff7421bfbfc87e6ed0ed2c83ccd3ccd36f3b7c58 Mon Sep 17 00:00:00 2001 From: Yash Patel Date: Sat, 16 Oct 2021 15:26:33 +0530 Subject: [PATCH] Create Rotate_Image.go Add solution for Leetcode Question 48. Roatate Image --- GO/Rotate_Image.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 GO/Rotate_Image.go diff --git a/GO/Rotate_Image.go b/GO/Rotate_Image.go new file mode 100644 index 00000000..5650ea60 --- /dev/null +++ b/GO/Rotate_Image.go @@ -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 + } + } +}