[LeetCode] Rotate Image n-by-n矩阵顺时针旋转
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?
一题严格的数组逻辑操作问题,考虑认真就好,思路:
| 1 | 2 | 3 | 1 |
| 3 | 4 | 4 | 2 |
| 2 | 4 | 4 | 3 |
| 1 | 3 | 2 | 1 |
如上表,一共有两圈,对于n 来说,圈数为(n+1)/2,从外圈到内圈,转换了一圈后到下一圈。
#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矩阵顺时针旋转的更多相关文章
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- Rotate Image,N*N矩阵顺时针旋转90度
public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && mat ...
- LeetCode Rotate Image (模拟)
题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...
- 将n*n矩阵顺时针旋转90度
/** * 将n*n矩阵顺时针旋转90度 * @param mat * @param n 矩阵的阶数 * @date 2016-10-7 * @author shaobn */ public stat ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- LeetCode——Rotate Image
1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...
- [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 ...
随机推荐
- cena 测评机下载地址
以下是cane的下载地址,现在分享给你们,希望有所帮助 下载地址百度云:https://pan.baidu.com/s/1JBXiVSZy-jhIc0V-F2ESPA 密码:hgtk 点击下载即可. ...
- Python自学笔记_
1. if语句 判断语句. 1 a=2 2 b=3 3 if a>b: 4 print("a>b") 5 else: 6 print("a<b" ...
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之enhanced coding styles
1.ANSI style 的代码比较紧凑. 下面规范推荐,比较好. 下面是带有parameter的module header的完整规范 一般1bit ,大家都是wire signal1 = gen_s ...
- JAVA基础篇—异常
五种常见异常 1.NullPointerException 空指针 2.ClassNotFoundException 指定类不存在 3.ArithmeticException运算异常 4.ArrayI ...
- POJ:3020-Antenna Placement(二分图的最小路径覆盖)
原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...
- ADT操作实例
ps.1.put(rank,value) 把当前rank的元素的数值修改 2.get(rank)获取rank的元素 3.remove(value)把向量中value剔除掉 4.size()返回元素个数 ...
- 使用docker+tomcat部署jenkins
- 光学字符识别OCR-6 光学识别
经过前面的文字定位和文本切割,我们已经能够找出图像中单个文字的区域,接下来可以建立相应的模型对单字进行识别. 模型选择 在模型方面,我们选择了深度学习中的卷积神经网络模型,通过多层卷积 ...
- python面试题解析(网络编程与并发)
1.答: 应用层 与其它计算机进行通讯的一个应用,它是对应应用程序的通信服务的.例如,一个没有通信功能的字处理程序就不能执行通信的代码,从事字处理工作的程序员也不关心OSI的第7层.但是,如果添加了一 ...
- loj2031 「SDOI2016」数字配对
跑最大费用最大流,注意到每次 spfa 出来的 cost 一定是越来越少的,啥时小于 \(0\) 了就停了吧. #include <iostream> #include <cstri ...