LeetCode:Rotate Image
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?
不使用额外的空间顺时针旋转方阵90度
例如
旋转后变为
算法1
先将矩阵转置,然后把转置后的矩阵每一行翻转
转置变为 每一行翻转变为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution {
public :
void rotate(vector<vector< int > > &matrix) {
int n = matrix.size();
//转置
for ( int i = 0; i < n; i++)
for ( int j = i+1; j < n; j++)
swap(matrix[i][j] , matrix[j][i]);
//每一行翻转
for ( int i = 0; i < n; i++)
for ( int j = 0; j < (n>>1); j++)
swap(matrix[i][j], matrix[i][n-j-1]);
}
};
|
算法2
可以见矩阵看成多个环组成,如下4*4的矩阵包括两个环,第一个环为1,2,3,4,8,12,16,15,14,13,9,5,1,第二个环为6,7,11,10。
旋转一个矩阵,相当于把每一个环都旋转。如何旋转一个环呢?以最外层的环举例: 本文地址
旋转前: ,旋转后:
我们把环分成3组:{1,4,16,13},{2,8,15,9},{3,12,14,5},每一组中:旋转后相当于把原来的数字移动到同组中下一个数字的位置
对于一个n*n的矩阵可以分成n/2(向上取整)个环来旋转;对于边长为len的环,可以分成len-1组来旋转。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Solution {
public :
void rotate(vector<vector< int > > &matrix) {
int n = matrix.size();
if (n == 0) return ;
for ( int i = 0, len = n; i < n/2; i++, len -= 2)
{ //n/2 为旋转的圈数,len为第i圈中正方形的边长
int m = len - 1;
for ( int j = 0; j < m; j++)
{
int tmp = matrix[i][i+j];
matrix[i][i+j] = matrix[i+m-j][i];
matrix[i+m-j][i] = matrix[i+m][i+m-j];
matrix[i+m][i+m-j] = matrix[i+j][i+m];
matrix[i+j][i+m] = tmp;
}
}
}
};
|
LeetCode:Rotate Image相关推荐
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 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 ...
- LeetCode Rotate Image(矩阵的旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- [LeetCode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- 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 ...
- leetcode Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [LintCode/LeetCode] Rotate List
Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...
- LeetCode Rotate Function(寻找规律)
题意:给出一个数组a,长度为n 其中f(0)=0*a[0]+1*a[1]+...+(n-1)*a[n-1] f(1)=0*a[n-1]+1*a[0]+...+(n-1)*a[n-2] ... f(n- ...
最新文章
- ptrace原理与性能对比
- golang并发和并行
- SQL Server 设置编辑所有行
- 之江杯2020零样本目标分割题参赛总结
- boost::unique_copy相关的测试程序
- 一种非常实用的系统掉电检测和保护电路---摘自:周立功单片机
- Django从理论到实战(part3)--创建一个Django项目
- 宝塔linux面板假设nextcloud,宝塔面板部署NextCloud(14.0.3)逐一解决后台安全及设置警告...
- w3c html规范规范文档,前端开发规范
- 使Eclipse下支持编写HTML/JS/CSS/JSP页面的自动提示。
- docker nginx部署web应用_实战docker,编写Dockerfile定制tomcat镜像,实现web应用在线部署...
- java验证邮件正则
- Excel表格数据生成ECharts图表
- 使用MATLAB绘制Smith圆图
- 西电计算机科学与技术学院王,王书振 - 西安电子科技大学 - 计算机科学与技术学院...
- win7家庭版如何升级到专业版和旗舰版
- Vue3+Vite项目配置Eslint+Prettier+Husky+Lint-Staged+Commitlint
- springmvc接收请求参数(springmvc教程二)
- DUL Oracle Data Unloader工具下载
- 华为防火墙通用配置详解
热门文章
- VS2013 int main(int argc, char** argv)参数传递
- 4011-基于邻接表的深度优先遍历(C++,取巧做法)
- linux生成不能访问的文件夹,Linux ln创建软连接之后无法使用,无法whereis
- php之time的用法,php中time()与$_SERVER[REQUEST_TIME]用法区别
- 计算机指纹识别的原理步骤,指纹识别原理和过程
- Mysql5.7.26解压版(免安装版)简单快速配置步骤,5分钟搞定(win10-64位系统)
- [Web]Restful风格的适用场景
- 前端工程师面试问题归纳(三、代码类)
- C#实现多级子目录Zip压缩解压实例
- 从资源管理器中,获取被选择的文件的路径(及文件夹)的API