LeetCode:Rotate Image
2016-01-23 21:00
253 查看
Rotate Image
My SubmissionsQuestion
Total Accepted: 58066 Total
Submissions: 172733 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?
Hide Tags
Array
思路:
可以自己拿张方形的纸试一下:
1.先将纸沿副对角线(右上到左下)对折;
2.再将纸沿中心横线对折,即为纸顺时针逆转90度后的结果。
code:
class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i+j <n) { int tmp = matrix[i][j]; matrix[i][j] = matrix[n-j-1][n-i-1]; matrix[n-j-1][n-i-1] = tmp; } } for(int i=0;i<n/2;i++) for(int j=0;j<n;j++) { int tmp = matrix[i][j]; matrix[i][j] = matrix[n-i-1][j]; matrix[n-i-1][j] = tmp; } } };
相关文章推荐
- win2003开启telnet
- android 人脸检测
- Spring MVC 生成EXCEL
- 一个Java写的批量重命名文件小程序
- 2、React Native中的flexbox布局
- Unity实现残影效果
- Job for httpd.service failed. See 'systemctl status httpd.service'
- ubuntu 15.10 install nvidia driver
- 简易的Java拼图游戏
- leetcode之Contains Duplicate II
- CDATA标记的作用
- 微信蓝牙开发板体验和开发指引
- LTP 源码分析- Doc目录下的一些有用文档
- spring mvc和web-flow的整合方案
- C语言 hdoj1002
- Mac 终端命令运行java
- 网页制作学习3----实现连缀功能
- GitHub&Git初级
- 查找第n小的元素
- Baby Ming and Matrix games(BC)