Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! π»π₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! π
100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney
Problem:
https://www.geeksforgeeks.org/problems/transpose-of-matrix-1587115621/1
Transpose of Matrix
Difficulty: Easy Accuracy: 66.5%
You are given a square matrix of size n x n. Your task is to find the transpose of the given matrix.
The transpose of a matrix is obtained by converting all the rows to columns and all the columns to rows.
Examples :
Input: mat[][] = [[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4]]
Output: [[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]]
Explanation: Converting rows into columns and columns into rows.
Input: mat[][] = [[1, 2],
[9, -2]]
Output: [[1, 9],
[2, -2]]
Explanation: Converting rows into columns and columns into rows.
Constraints:
1 β€ n β€ 103
-109 β€ mat[i][j] β€109
Solution:
class Solution:
def transpose(self, mat):
n = len(mat)
for i in range(n):
for j in range(i+1, n):
mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
return mat
Top comments (0)