preface

Originally today thinking of a problem can be solved, did not expect everyone is so fierce, so while still waiting for the release of another write! 😄

Topic describes

48. Rotate the image

Given an n by n two-dimensional matrix matrix represents an image. Please rotate the image 90 degrees clockwise.

You have to rotate the image in place, which means you need to modify the input two-dimensional matrix directly. Please do not use another matrix to rotate the image.

Example 1:

Input: matrix = [[1, 2, 3], [4 and 6], [7,8,9]] output: [,4,1 [7], [8,5,2], [9,6,3]]Copy the code

Example 2:

Input: matrix = [,1,9,11 [5],,4,8,10 [2], [13,3,6,7], [15,14,12,16]] output: [,13,2,5 [15], [14,3,4,1], [12,6,8,9], [16,7,10,11]]Copy the code

Example 3:

Input: matrix = [[1]] Output: [[1]Copy the code

Example 4:

Input: matrix = [1,2],[3,4]] output: [[3,1],[4,2]]Copy the code

Tip:

  • matrix.length == n
  • matrix[i].length == n
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000

Their thinking

When I first saw the title, I had no idea. After READING it, I felt oh, and then there was no idea. What should I do if I had no idea?

Take out small notebook, with examples to find methods, looking for ideas, is all kinds of random try, in fact, I do not recommend such, mainly I am in is no idea, more look more sleepy, so use this method. Even if it’s wrong, it’ll keep me focused.

So step one, how do I swap 1 and 7? Let’s flip the array.

At this time, the matrix is as follows:

[[7, 8, 9], [4, 5, 6], [1, 2, 3]Copy the code

Well, you’ve done what you want in step one, and then what? How do I swap 4 with 8, 1 with 9?

Try switching places, and the results come out…

Click Submit.

The problem solving code

var rotate = function(matrix) {
    matrix.reverse();
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < i; j++) {
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]
        }
    }
    return matrix;
};
Copy the code

Swipe questions and punch out records

Here is the previous swipe card records, you can have a look, if you have any different views and views or feel what is wrong, welcome to leave a message in the comment area! 🙏 🙏 🙏

[LeetCode0303 topic area and retrieval – array immutable] | punch brush

[LeetCode1200. Minimum absolute difference] | punch brush

[LeetCode0304 topic 2 d area and retrieval – matrix immutable] | punch brush

[LeetCode11 topic containers of most water] | punch brush

[LeetCode0338 topic bit count] | punch brush

[LeetCode209 topic length minimum subarray] | punch brush

[LeetCode236 topic in recent common ancestor of binary tree] | punch brush

[LeetCode141 topic circular linked list] | punch brush

[LeetCode53 topic most architectural sequence and] | punch brush

conclusion

It is necessary to write more questions, do not be afraid to write bad, the process of writing is the process of review, it is easier to deepen their understanding.

Come on! HXDM!!!!!! 💪 💪 💪

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign