ITPub博客

首页 > > > [LeetCode] Rotate Image

[LeetCode] Rotate Image

作者:Ruiyuan Li 时间:2015-05-22 00:00:00 0 删除 编辑

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度,相当于先将图片沿着x=n/2翻转,然后再将图片沿着斜对角线y=x翻转。代码如下:

class Solution {
public:
    void rotate(vector>& matrix) {
        int len = matrix.size();
        //先将矩阵左右翻转
        for(int i = 0; i& v){
        int len = v.size();
        int i=0, j=len-1;
        while(i& v, int i, int j){
        int temp = v[i];
        v[i] = v[j];
        v[j] = temp;
    }
};


转载请注明: » [LeetCode] Rotate Image
<!-- -->

原文作者:;如有侵权,请联系管理员删除。

上一篇: Kangry学Python
请登录后发表评论 登录
全部评论

注册时间:2020-05-21

  • 博文量
    495
  • 访问量
    4824