码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Rotate Image

时间:2014-12-25 11:18:52      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:algorithm   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?


思路:先将行顺序reverse,然后再对每个处于下三角区域的元素rotate。


代码:

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

    reverse(matrix.begin(),matrix.end());
    for(int i = 0;i < matrix.size();i++)
        for(int j = 0;j < matrix.size() - i;j++)
        {
            int temp = matrix[j][i];
            matrix[j][i] = matrix[i][j];
            matrix[i][j] = temp;
        }
}


LeetCode:Rotate Image

标签:algorithm   leetcode   

原文地址:http://blog.csdn.net/yao_wust/article/details/42143319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!