DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

1 1 1 1

Leetcode - 48. Rotate Image

/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var rotate = function (matrix) {

    let l = 0, r = matrix.length-1;

    while (l < r) {

        for (let i = 0; i < r - l; i++) {

            let top = l, bottom = r;

            let saveTopLeft = matrix[top][l + i];

            //move bottomLeft to topLeft
            matrix[top][l + i] = matrix[bottom - i][l];

            //move bottomRight to bottomLeft
            matrix[bottom - i][l] = matrix[bottom][r - i];


            //move topRight to bottomLeft
            matrix[bottom][r - i] = matrix[top + i][r];


            //saved to topRight
            matrix[top + i][r] = saveTopLeft

        }
        l++;
        r--;
    }

};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

šŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ā¤ļø or leave a quick comment to share your thoughts!

Okay