<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
- <LeetCode OJ> 48. Rotate Image
- LeetCode OJ:Rotate Image
- LeetCode OJ - Rotate Image
- [LeetCode OJ]Rotate Image
- LeetCode OJ Rotate Image
- LeetCode OJ-48-Rotate Image
- [LeetCode]48.Rotate Image
- LeetCode 48. Rotate Image
- LeetCode 48.Rotate Image
- LeetCode --- 48. Rotate Image
- [Leetcode] 48. Rotate Image
- [leetcode] 48. Rotate Image
- Leetcode 48. Rotate Image
- 48. Rotate Image LeetCode
- [LeetCode]48. Rotate Image
- leetcode 48. Rotate Image
- Leetcode 48. Rotate Image
- LeetCode 48. Rotate Image
- 部门沟通规范
- 好吧,这又是一个圆形进度条!
- virtualenv开发环境配置
- Could not publish server configuration for Tomcat v7.0 Server at localhost.
- 提高工作效率
- <LeetCode OJ> 48. Rotate Image
- Spring AOP 实现原理与 CGLIB 应用
- 跟我一起学C++之从C到C++(bool类型)
- php链接数据语句
- 一道简单的题被做的很难
- 关于genymotion安装的一些问题汇总
- 提高zxing生成二维码的容错率及zxing生成二维码的边框设置
- LightOJ 1294 Positive Negative Sign 求和的升级版 暑期练习T
- 栈帧