LeetCode[Array]: 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?

参考LeetCode[Array]: Spiral Matrix II的迭代思路,先完成最外环的旋转,然后依次旋转内环。我的C++代码如下:

void rotate(vector<vector<int> > &matrix) {
    int beginRow = 0, endRow = matrix.size() - 1, beginCol = 0, endCol = matrix[0].size() - 1;
    while (beginRow <= endRow && beginCol <= endCol) {
        for (int i = 0; i < endCol - beginCol; ++i) {
            int tmp                               = matrix[beginRow    ][beginCol + i];
            matrix[beginRow    ][beginCol + i] = matrix[  endRow - i][beginCol      ];
            matrix[  endRow - i][beginCol    ] = matrix[  endRow    ][  endCol - i];
            matrix[  endRow    ][  endCol - i] = matrix[beginRow + i][  endCol    ];
            matrix[beginRow + i][  endCol    ] = tmp;
        }

        ++beginRow; --endRow;
        ++beginCol; --endCol;
    }
}

在Discuss上看到一个比较奇特的解法,其思路是:首先将矩阵上下倒置,然后沿左上到右下的对角线对折即可完成旋转,其C++代码实现非常简洁:

void rotate(vector<vector<int> > &matrix)
{
    reverse(matrix.begin(), matrix.end());
    for (unsigned i = 0; i < matrix.size(); ++i)
        for (unsigned j = i+1; j < matrix[i].size(); ++j)
            swap (matrix[i][j], matrix[j][i]);
}
时间: 2024-07-29 15:50:36

LeetCode[Array]: Rotate Image的相关文章

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 189. Rotate Array 数组

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

【LeetCode】Rotate Array

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

[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

题目链接: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<

[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[Array]: Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这个问题比较简单. vector<int> getRow(int rowIndex) { vector<int> row, prev