【leetcode刷题笔记】Rotate Image
2024-08-28 15:52:09
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?
题解:
如果要原地算法的话,就只能用交换实现了。首先把matrix[i][j]和matrix[j][i]交换,然后把整个矩阵左右翻转,如下图所示:
1 2 3 1 4 7 7 4 1
4 5 6 2 5 8 8 5 2
7 8 9 3 6 9 9 6 3
JAVA代码:
public 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 temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
for(int j = 0;j < n/2;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n-j-1];
matrix[i][n-j-1] = temp;
}
}
}
}
最新文章
- react 表单
- 【leetcode】Simplify Path
- 深入DNS
- ABAP开发基础知识:内表(Internal Table)
- win8如何删除未知账户(s-1-5-21-2000478354-1390067357-725345543-1003)
- [JS][jQuery]remove()与 empty()的差别
- SQL Server索引设计 <;第五篇>;
- Selenium2Library使用Remote功能(转载并更新)
- js-时间函数相互转化
- 神经网络ANN——SPSS实现
- Kubernetes系列之Helm介绍篇
- css字体图标的使用方法
- Mac截图操作,自定义快捷键
- 还能不能愉快地起一个web服务啦?——1st Step!白话http和代码交互的那点儿事儿~
- Final阶段第1周/共1周 Scrum立会报告+燃尽图 06
- C/s程序过时了吗?
- List特有迭代器--ListIterator的特殊功能
- LeetCode 12 Integer to Roman (整数转罗马数字)
- .net GUI框架
- 使用Pelican在Github(国外线路访问)和Coding(国内线路访问)同步托管博客
热门文章
- js 风格(注意事项)
- Objective-C中的instancetype和id关键字(转)
- Deep Networks for Image Super-Resolution with Sparse Prior
- hashMap与hashTable区别
- 7.解决谷歌的SDK更新失败问题。
- 对IOS设备中UDID的一些思考
- C#中引用类型和值类型分别有哪些
- JZOJ.5234【NOIP2017模拟8.7】外星人的路径
- SPOJ OPTM - Optimal Marks
- Minecraft Forge编程入门一 “环境搭建”