[LeetCode] Rotate Image
来源:互联网 发布:淘宝erp是什么意思 编辑:程序博客网 时间:2024/06/08 14:58
题目:
You are given an n×n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up: Could you do this in-place?
分析:
要达到in-place的要求,我们采用交换的方法,顺序是左上先和左下交换,然后左上和右下交换,然后左上和右上交换。
代码:
public class RotateImage {public void rotate(int[][] matrix) {//计算圈数int n = matrix.length, lvl = n / 2;for(int i = 0; i < lvl; i++) {for(int j = i; j < n - i - 1; j++) {//左上和左下交换swap(matrix, i, j, j, n - i - 1);//左上和右下交换swap(matrix, i, j, n - i - 1, n - j - 1);//左上和右上交换swap(matrix, i, j, n - j - 1, i);}}}private void swap(int[][] matrix, int i1, int j1, int i2, int j2) {int tmp = matrix[i1][j1];matrix[i1][j1] = matrix[i2][j2];matrix[i2][j2] = tmp;}}
阅读全文
0 0
- LeetCode: Rotate Image
- [Leetcode] Rotate Image
- LeetCode : Rotate Image
- [LeetCode] Rotate Image
- leetcode 67: Rotate Image
- [leetcode] Rotate Image
- [Leetcode] Rotate Image
- [LeetCode]Rotate Image
- LeetCode-Rotate Image
- [leetcode] Rotate Image
- LeetCode - Rotate Image
- leetcode之Rotate Image
- LeetCode - Rotate Image
- Leetcode: Rotate Image
- LeetCode:Rotate Image
- Leetcode: Rotate Image
- Leetcode Rotate Image
- leetcode Rotate Image
- mysql数据库的完整备份、差异备份、增量备份
- Linux下堡垒机环境部署记录(1)-Jumpserver安装
- warning: function declaration isn’t a prototype(函数声明不是原型)的解决办法
- HTTP头部信息
- 【菜鸟学框架】——Struts2快速上手
- [LeetCode] Rotate Image
- A Knight's Journey POJ
- Java I/O流总结
- HDU
- CSS3背景相关新增属性
- tensorflow导入报错ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
- iframe 页面跳转刷新父页面窗口
- 串口开发类
- maven用jetty启动工程