[C++]LeetCode: 68 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*N的图片的90度旋转(顺时针)

Answer 1: 中心旋转法

思路:根据题意,我们知道图片旋转是根据中心进行旋转的,于是我们将图片根据两条对角线划分为四个区域: A, A‘, A",A"‘.然后依次替换四个位置的值,维护左上角的值。我们要做的就是将上边替换到右边,右边替换到下边,下边替换到左边。实际上完成的就是A ->A‘->A"->A"‘->A
,实现四个区域的替换。所以外层循环从0~matrix.size()/2, 内层循环根据对角线从i ~ matrix.size() - i -1.我们唯一要注意的就是替换左边的确定,具体可以画图得到。可以参考如下的图加以理解。根据旋转中不变的长度来判断。

AC Code:

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        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;
            }
        }

        return;
    }
};

Answer 2:
转置法 (程序更简洁)

思路1:先得到原矩阵的转置矩阵,之后从转置矩阵到旋转矩阵再reverse每行的元素即可。

思路2:先上下折叠原矩阵,再计算折叠矩阵的转置矩阵,即所求矩阵。

有图好理解,矩阵如下图:

Attention:

求转置矩阵时,将下三角矩阵和上三角矩阵元素交换。注意循环的里外层范围确定。

 for(int i=0,n=matrix.size();i<n;++i){
            for(int j=i+1;j<n;++j)  

AC Code1:

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        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());
        }

        return;
    }
};

AC Code2:

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n = matrix.size();
        reverse(matrix.begin(), matrix.end());

        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                swap(matrix[i][j], matrix[j][i]);
            }
        }

        return;
    }
};

时间: 2024-08-08 23:27:05

[C++]LeetCode: 68 Rotate Image的相关文章

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 --- 68. Text Justification

题目链接:Text Justification Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you

【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<

[LeetCode 题解]: 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个元素移动到链表的头部. 这道题的本质就是寻找链表的

【LeetCode】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. start with an example. Given [0,1,2], rotate 1 steps to the right -&

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

[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]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

leetcode 61 Rotate List ----- java

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,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len