Rotate Image
来源:互联网 发布:java ssl 双向认证 编辑:程序博客网 时间:2024/06/06 17:46
题目描述:
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
解题思路:顺时针旋转90度的规律:a[i,j]=>a[j,cols-i],即a[i,j]移动到a[j,cols-i]的位置。
一圈一圈的对数据进行旋转处理,先处理最外圈的,然后是次外圈,...
使用变量m表示当前所处理圈的每一行的元素的数目,如果原始输入矩阵的行(或列)为偶数,m最后为0,
如果原始输入矩阵的行(或列)为奇数,m最后为1,而最后这一圈只包含一个元素,不需要处理,所以以m>1作为循环继续的条件,
每处理完一圈数据,m=m-2.
对于每一圈数据的处理,需要处理m-1个数据,每一个数据的旋转影响另外三个数据的旋转
AC代码如下:
class Solution {public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size();if (n <= 0) return;int count = 0;int m = n;while (m > 1){for (int i = 0; i < m - 1; i++){int a = count;int b = count;int cur = 0;int next = matrix[b+i][n - 1 - a];matrix[b+i][n - 1 - a] = matrix[a][b+i];cur = next;int tmp = n-1-a;a = b+i;b = tmp;for (int j = 0; j < 3; j++){next = matrix[b][n - 1 - a];matrix[b][n - 1 - a] = cur;cur = next;int tmp = n - 1 - a;a = b;b = tmp;}}count++;m = m - 2;} }};
0 0
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- cf#336-B-Hamming Distance Sum 前缀和
- Java中Synchronized的用法
- ios app的在线调试,查看日志
- CodeForces 605E Intergalaxy Trips (概率)
- C#操作MSMQ向本地消息队列发送和接收消息
- Rotate Image
- Java中JDBC的PreparedStatement用法
- Andrew NG 《machine learning》week 7,class1 —Large Margin Classification
- bzoj1055[HAOI2008]玩具取名 (区间dp)
- C语言及shell描述符重定向
- 简易新闻客户端(生成列表)
- 位运算【C++学习(计蒜客)】
- hdu 2089 -不要62(数位 dp)
- Single Number III