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 3 1
3 4 4 2
2 4 4 3
1 3 2 1

如上表,一共有两圈,对于n 来说,圈数为(n+1)/2,从外圈到内圈,转换了一圈后到下一圈。

 
我写的代码用了长度len ,其实写j=i 会方便很多。
需要计算好下标,有个简单的确认,一次转换中的4个数,不相邻的两个数的对应下标和为定值(n-1),例如一个是<i,j>,另一个就是<n-1  -i,n-1  -j>,利用这个规律其实只要确认两个就行。
代码如下:
 #include <iostream>
#include <vector>
#include <iterator>
using namespace std; class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n =matrix.size();
if(n<) return ;
for(int i=;i<(n+)/;i++){
for(int len=;len<=n-*(i+);len++){
int temp = matrix[i][i+len];
matrix[i][i+len] = matrix[n--i-len][i];
matrix[n--i-len][i] = matrix[n--i][n--i-len];
matrix[n--i][n--i-len] = matrix[i+len][n-i-];
matrix[i+len][n-i-] = temp;
}
}
}
}; int main()
{
vector<vector<int> > matrix;
vector<int> tempm;
int cnt =;
for(int i=;i<;i++){
for(int j=;j<;j++)
tempm.push_back(cnt++);
matrix.push_back(tempm);
tempm.clear();
}
Solution sol;
sol.rotate(matrix);
for(int i=;i<matrix.size();i++){
copy(matrix[i].begin(),matrix[i].end(),ostream_iterator<int>(cout," "));
cout<<endl;
} return ;
}
 

[LeetCode] Rotate Image n-by-n矩阵顺时针旋转的更多相关文章

  1. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  2. Rotate Image,N*N矩阵顺时针旋转90度

    public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...

  3. LeetCode Rotate Image (模拟)

    题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...

  4. 将n*n矩阵顺时针旋转90度

    /** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  8. LeetCode——Rotate Image

    1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...

  9. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

随机推荐

  1. 使用max函数计算EXCEL个税公式

    1.Max()函数是求括号内的数的最大值.2.其中,第一和第二个大括号{}内的数,相信作为财务的应该很清楚,就是个人所得税的缴税比例,以及速算个人应缴所得税的相关数据.3.在EXCEL中,使用{}表示 ...

  2. IOS7的变化

    API变化: 1.弃用 MKOverlayView 及其子类,使用类 MKOverlayRenderer: 2.弃用 Audio Toolbox framework 中的 AudioSession A ...

  3. Matlab学习记录(函数)

    Matlab中的内建函数 Matlab自定义函数 用function构造函数 用inline构造函数 用syms构造符号函数 多项式相关函数 polyvalx convx 向量和矩阵运算函数 向量运算 ...

  4. UML类图中的六种线条与六种关系

    1.泛化(generalize) 实线空心三角箭头. 2.实现(realize) 虚线空心三角箭头. 3.聚合(aggregation) 实线空心菱形箭头. 4.组合(composition) 实线实 ...

  5. 初涉manacher

    一直没有打过……那么今天来找几道题打一打吧 manacher有什么用 字符串的题有一类是专门关于“回文”的.通常来说,这类问题要么和一些dp结合在一起:要么是考察对于manacher(或其他如回文自动 ...

  6. unbuntu14下Qt4.8 和MySQL连接问题 QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE

    使用 QSqlDatabase::addDatabase创建数据库时 会报错: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: availab ...

  7. LeetCode_6

    问题: 6. Z字形变换 链接:https://leetcode-cn.com/problems/zigzag-conversion/description/ 分析: A 仿真方法 直接模拟整个过程, ...

  8. ssl 在nginx上的部署示例

    server { listen 80; listen 443 ssl; server_name           [DOMAIN]; ssl on; ssl_certificate /work/ss ...

  9. HDU 4781 Assignment For Princess 构造

    题意: 构造一个\(N(10 \leq N \leq 80)\)个顶点\(M(N+3 \leq M \leq \frac{N^2} {7})\)条边的有向图,要满足如下条件: 每条边有一个\([1,M ...

  10. TextView设置缩略显示

    1.代码设置 textview.setSingleLine(); textview.setEllipsiz(TextUtils.TruncateAt.valueOf("END")) ...