LeetCode 48. Rotate Image (Solution Explained)

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

This makes much more sense than the CTCI solution explanation!

luiscortes
Автор

You are the kind of tech youtuber that I didn't know I needed.
Thank you for doing videos like these. I have been consuming them alot lately.
Highly informational!

Xyvier
Автор

Great! I was stuck until I found a pattern and solved it recursively. But this (your method) is way simpler.

I'll put my solution in case someone looking for a pattern.

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {

helper(matrix, matrix.size(), 0 );

}



void helper(vector<vector<int>>& mat, int size, int start) {

if(size <= 1)
return;

int i = start, j = start, ptr = 0, n = start + size;
while(j < n-1) {

swap(mat[i+ptr][n-1], mat[i][j]);
swap(mat[n-1][n-1-ptr], mat[i][j]);
swap(mat[n-1-ptr][i], mat[i][j]);
j++;
ptr++;

}
helper(mat, size-2, start + 1);
// I am reducing the size by 2, to recurse and loop through inner matrix.
}
};

baibhabmondal
Автор

Wow! I read some articles and tried following some examples but only after your explanation, I was able to fully understand the concept and the tricks! Thank you so so much!

NuschStephany
Автор

Daymnnn Man you made it so easy !! You have the best solutions for leetcode on youtube

Asingh
Автор

Brother, Superb explanation ! Earlier I thought of the same approach but got stuck in the code and your video helped me in a perfect way.

bharatprakashparakh
Автор

I felt stuck then I watched this video. Feel alive again!!

monicawang
Автор

of all the explanations ive seen of this problem, this explanation makes the most since.

JustThink
Автор

Great explanation! I had a variant of this problem yesterday in an OA, and I didn't figure out how to rotate the square matrix. wish I had watched your video earlier lol! Keep the good videos coming!

xinyucao
Автор

We can simplify the 2nd loop little better like
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length <= 1) {
return;
}
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < n; i++) {
int head = 0;
int tail = n - 1;
while (head < tail) {
int temp = matrix[i][head];
matrix[i][head] = matrix[i][tail];
matrix[i][tail] = temp;
head++;
tail--;
}
}
}

rakshith
Автор

You can also create a matrix and multiply to get the desired result
i -row, j-column

for position i=i, j=0 - multiply i-th column with [0 0 1]
for position i=i, j=1 - multiply i-th column with [0 1 0]
for position i=i, j=2 - multiply i-th column with [1 0 0]

we can also think parameterizing the multiplication matrix as well. Size of the multiplication matrix is dependent on the size of the original matrix

cracknlp
Автор

I'm glad I didn't waste much time thinking about the solution on my own cuz I would have never guessed this

adarshsasidharan
Автор

I was stuck on this problem from 3days and solved on my own but this approach is much better

random-
Автор

Thanks a lot! I watched the different solution with actual rotating, but your aproach looks more sophisticated. I think the description force you to do like this showing several matrixes with swapped rows and columns

Sergey
Автор

Great explanation, really helped a lot. Love from India.

shiprashakya
Автор

Oh god thanks for this solution. The two available on LeetCode solutions page and in some other videos are either complex or not explained properly. Finally got this into my head.

BrajBliss
Автор

I think that in the transpose part, you can add one condition if(i != j) because the diagonal elements don't need to be transposed.

huizhao
Автор

Good explanation.Finally i understood the approach.

BadriBlitz
Автор

Amazing explanation! Thank you very much

MP-nyep
Автор

Thanks, man! Great Logic. It kind of reminds of Linear Algebra : )

ashishdukare