[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 ...
随机推荐
- bash编程之循环控制:
bash编程之循环控制: for varName in LIST; do 循环体 done while CONDITION; do 循环体 done until CONDITION; do 循 ...
- Roman Numeral Converter-freecodecamp算法题目
Roman Numeral Converter 1.要求 将给定的数字转换成罗马数字 所有返回的罗马数字都应该是大写形式 2.思路 分别定义个位.十位.百位.千位的对应罗马数字的数组 用Math.fl ...
- CF-629 D - Babaei and Birthday Cake (离散化 + 线段树|树状数组)
求上升子序列的最大和.O(n^2)会暴力,在查询的时候要用线段树维护 因为权值是浮点数,故先离散化一下,设第 i 个位置的权值,从小到大排名为 id.那么dp转移中 \[d[i] = max(d[i] ...
- Bzoj 1131[POI2008]STA-Station (树形DP)
Bzoj 1131[POI2008]STA-Station (树形DP) 状态: 设\(f[i]\)为以\(i\)为根的深度之和,然后考虑从他父亲转移. 发现儿子的深度及其自己的深度\(-1\) 其余 ...
- VNC远程登录端使用经验之一
1.vnc/xmanager都是经常用的远程登录软件.vnc有个缺点就是他的进程不会自动退出比如如果开了PID1再去开PID2...PIDn.那么前面的PIDn-1个进程就会一直运行如果不手动kill ...
- leetcode-27-exercise_bit maniputation
461. Hamming Distance 解题思路: 把两个数的每一位和1比较,如果结果不同说明这两位不同.要比较32次. int hammingDistance(int x, int y) { i ...
- BFS:UVa1590-IP Networks (子网掩码相关知识)
IP Networks Alex is administrator of IP networks. His clients have a bunch of individual IP addresse ...
- selenium2用AutoIt上传文件
1.标签是input,如下图所示: WebElement e1= driver.findElement(By.id("load"));//输入要上传文件的地址e1.sendKeys ...
- linux+ARM学习路线
学习步骤如下: 1.Linux 基础 安装Linux操作系统 Linux文件系统 Linux常用命令 Linux启动过程详解 熟悉Linux服务能够独立安装Linux操作系统 能够熟练使用Linux系 ...
- STF 连接其它操作系统上的安卓设备实操介绍【转】
功能简介:https://www.jianshu.com/p/464fadaeb1d7 搭建教程:https://blog.csdn.net/xl_lx/article/details/7944586 ...