【一天一道LeetCode】#48. Rotate Image

一天一道LeetCode系列

(一)题目

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

(二)解题

90度旋转图像,我们不难看出

matrix[i][j]=tmp[j][n?i?1]注:tmp=matrix

经过这样的变换后,图像就旋转了90度。



class Solution {

public:

    void rotate(vector<vector<int>>& matrix) {

        int n = matrix.size()-1;

        vector<vector<int>> tmp = matrix;//深拷贝

        for(int i = 0 ; i< n+1; i++)

        {

            for(int j = 0 ; j < n+1 ; j++)

            {

                matrix[j][n-i] = tmp[i][j];//赋值转换

            }

        }

    }

};

网上还有一种解法去先转置在对称变换。代码如下:


class Solution {

public:

    void rotate(vector<vector<int> > &matrix) {

        int dim = matrix.size();

        int temp = 0;

        for (int i = 0; i < dim; ++i) { //先转置

            for (int j = i+1; j < dim; ++j) {

                temp = matrix[i][j];

                matrix[i][j] = matrix[j][i];

                matrix[j][i] = temp;

            }

        }

        for (int i = 0; i < dim/2; ++i) { //然后对称变换

            for (int j = 0; j < dim; ++j) {

                temp = matrix[j][i];

                matrix[j][i] = matrix[j][dim - i -1];

                matrix[j][dim - i -1] = temp;

            }

        }

    }

};
时间: 2024-07-30 09:04:56

【一天一道LeetCode】#48. Rotate Image的相关文章

LeetCode 48. Rotate Image(旋转图像)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 题目标签:Array 这道题目给了我们一个n * n的矩阵,让我们旋转图片.题目要求in-place,所以就不能用额外的空间了.一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢.只好去看看别人的方法,看完觉得,自己

Leetcode 48 Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat

LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazing!".(forgivig my poor English!) 代码如下(代码怎么写已经不重要了!): class Solution { public: void rotate(vector<vector<int>>& matrix) { int r=matrix.

leetCode 48.Rotate Image (旋转图像) 解题思路和方法

Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:其实就是旋转数组,没有什么难度,代码如下: public class Solution { public void rotate(int[][] matrix) { int[][] a =

LeetCode 48. Rotate Image (C++)

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: 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

19.2.7 [LeetCode 48] Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: 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

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

【一天一道LeetCode】#61. Rotate List

一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. (二)解题 本题的思路: 1.找到倒数第K个节点 2.将k以后的节点移动到前面来,与头结点

LeetCode --- 61. Rotate List

题目链接:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不