<LeetCode OJ> 48. Rotate Image

来源:互联网 发布:域名绑定ip 端口 编辑:程序博客网 时间:2024/05/18 07:13

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium

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>>& matrix) {        int n=matrix.size();        //一,对角线对换        for(int i=0;i<n-1;i++)//列        {            for(int j=0;j<n-1-i;j++)//行            {                int tmp=matrix[j][i];                matrix[j][i]=matrix[n-1-i][n-1-j];                matrix[n-1-i][n-1-j]=tmp;            }        }        //二,水平线对换        for(int i=0;i<n;i++)//列        {            for(int j=0;j<n/2;j++)//行            {                int tmp=matrix[j][i];                matrix[j][i]=matrix[n-1-j][i];                matrix[n-1-j][i]=tmp;            }        }    }};

注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51588641

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

1 0
原创粉丝点击