048 Rotate Image 旋转图像
2024-09-29 20:11:36
给定一个 n × n 的二维矩阵表示一个图像。
将图像旋转 90 度(顺时针)。
注意:
你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像。
例 1:
给出的输入矩阵 =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
旋转输入矩阵,使其变为 :
[
[7,4,1],
[8,5,2],
[9,6,3]
]
例 2:
给出的输入矩阵 =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
旋转输入矩阵,使其变为 :
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
详见:https://leetcode.com/problems/rotate-image/description/
Java实现:
class Solution {
public void rotate(int[][] matrix) {
int n=matrix.length;
//沿左上至右下对角线,交换对称对
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=tmp;
}
}
//水平翻转每一行
for(int i=0;i<n;++i){
for(int j=0;j<n/2;++j){
int tmp=matrix[i][j];
matrix[i][j]=matrix[i][n-1-j];
matrix[i][n-1-j]=tmp;
}
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4389572.html
https://www.cnblogs.com/lightwindy/p/8564385.html
最新文章
- 对session的理解
- python中各种结构的复杂度
- 【项目经验】——JSON.parse() &;&; JSON.stringify()
- [LeetCode][Python]14: Longest Common Prefix
- python web
- leetcode第七题--Reverse Integer
- JavaScript语法作业
- C#基础知识之属性
- PHP字符串函数之 sscanf echo print sprintf vsprintf printf vprintf fprintf vfprintf
- 一本通1530 Ant Trip
- windows系统中搭建Jenkins服务器
- laravel 数据验证
- Linux安装redis和部署
- MAC Book 共享网络连接
- 关于less在DW中高亮显示问题
- webstorm添加调试nodejs
- blast及其格式输出简介
- TCP 3-Way Handshake (SYN,SYN-ACK,ACK)
- 彻底禁止win10更新
- java.lang.IncompatibleClassChangeError:可以考虑是否是jar包冲突
热门文章
- 使用top命令查看系统状态
- 机器学习:Colorization using Optimization
- zabbix3.2部署
- 无废话WCF系列教程 -- 李林峰
- POJ3067(树状数组:统计数字出现个数)
- The Tomcat server configuration at \Servers\Tomcat v8.0 Server at localhost-config is missing. Check the server for erro
- Entity Framework之领域驱动设计实践
- sorted matrix - search &; find-k-th
- HDU 3572 Task Schedule (最大流)
- SetROP2