[LeetCode]27. 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?

解法1:新开辟一个矩阵,空间复杂度O(n^2)。

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        vector< vector<int> > tmp(n, vector<int>(n, 0));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                tmp[j][n - 1 - i] = matrix[i][j];
        }
        for(int i = 0; i < n; i++)
            copy(tmp[i].begin(), tmp[i].end(), matrix[i].begin());
    }
};

解法2:矩阵顺时针旋转90°可以通过两个操作来完成:先将矩阵上下翻转,然后将矩阵转置(按左上-右下对角线翻转)。空间复杂度O(1)。

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < n; j++)
                swap(matrix[i][j], matrix[n - 1 - i][j]);
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < i; j++)
                swap(matrix[i][j], matrix[j][i]);
        }
    }
};

或者先转置,再左右翻转。reverse能放在第一层for循环内是因为前面进行的转置已经处理好一行了。

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                swap(matrix[i][j], matrix[j][i]);
            }
            reverse(matrix[i].begin(), matrix[i].end());
        }
    }
};

或者先按右上-左下对角线翻转,再上下翻转。注意按照右上-左下对角线翻转时对应坐标关系。

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j <n - 1 - i; j++)
                swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
        }
        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < n; j++)
                swap(matrix[i][j], matrix[n - 1 - i][j]);
        }
    }
};

或者先左右翻转,再按照右上-左下对角线翻转。

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        for(int i = 0; i < n; i++)
            reverse(matrix[i].begin(), matrix[i].end());
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n - 1 - i; j++)
                swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
        }
    }
};

解法3:3*3的矩阵可以看做两圈,中间一圈只有一个数字不需要动,只需将外面一圈顺时针旋转90°即可;4*4矩阵两圈,分别顺时针旋转90°即可。因此可以每次旋转一圈,一共需要旋转n/2圈,每圈旋转n-1个数字。

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        if(matrix.empty() || matrix[0].empty())
            return;
        int n = matrix.size();
        for (int i = 0; i < n / 2; i++) //处理矩阵的哪一圈
        {
            for (int j = i; j < n - 1 - i; j++) //每圈需要处理的元素个数
            {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = tmp;
            }
        }
    }
};
时间: 2024-10-21 08:57:39

[LeetCode]27. Rotate Image图像旋转的相关文章

LeetCode 189. Rotate Array (旋转数组)

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

LeetCode 189 Rotate Array(旋转数组)

翻译 通过K步将一个有着n个元素的数组旋转到右侧. 例如, 给定n = 7和k = 3,数组[1,2,3,4,5,6,7]会被旋转成[5,6,7,1,2,3,4]. 批注: 尽你可能尝试多种解决方案,这里至少存在3种不同的方式去解决这个问题. 原文 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotate

每日算法之三十七: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? 原地图像顺时针旋转90度.因为要求空间复杂度是常数,因此应该迭代旋转操作. class Solution { public: void rotate(vector<vector<int> > &mat

Leetcode: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. 解题分析: 不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转 对于链表的旋转,实际上

每日算法37: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? 原地图像顺时针旋转90度.由于要求空间复杂度是常数,因此应该迭代旋转操作. class Solution { public: void rotate(vector<vector<int> > &mat

[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][Java] 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? 题意: 给定一个 n x n 的二维矩阵,来表示一副图像. 把图像旋转90度(顺时针) 你能否在原矩阵上完成呢? 算法分析: 方法一: 空间复杂度较高,开辟额外的空间开复制原矩阵.但是最好理解 找到规律即可:mat

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个节点放到前面,可以采用快慢指针处理.不

【Leetcode】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? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<