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;
}
}
}
}

最新文章

  1. react 表单
  2. 【leetcode】Simplify Path
  3. 深入DNS
  4. ABAP开发基础知识:内表(Internal Table)
  5. win8如何删除未知账户(s-1-5-21-2000478354-1390067357-725345543-1003)
  6. [JS][jQuery]remove()与 empty()的差别
  7. SQL Server索引设计 &lt;第五篇&gt;
  8. Selenium2Library使用Remote功能(转载并更新)
  9. js-时间函数相互转化
  10. 神经网络ANN——SPSS实现
  11. Kubernetes系列之Helm介绍篇
  12. css字体图标的使用方法
  13. Mac截图操作,自定义快捷键
  14. 还能不能愉快地起一个web服务啦?——1st Step!白话http和代码交互的那点儿事儿~
  15. Final阶段第1周/共1周 Scrum立会报告+燃尽图 06
  16. C/s程序过时了吗?
  17. List特有迭代器--ListIterator的特殊功能
  18. LeetCode 12 Integer to Roman (整数转罗马数字)
  19. .net GUI框架
  20. 使用Pelican在Github(国外线路访问)和Coding(国内线路访问)同步托管博客

热门文章

  1. js 风格(注意事项)
  2. Objective-C中的instancetype和id关键字(转)
  3. Deep Networks for Image Super-Resolution with Sparse Prior
  4. hashMap与hashTable区别
  5. 7.解决谷歌的SDK更新失败问题。
  6. 对IOS设备中UDID的一些思考
  7. C#中引用类型和值类型分别有哪些
  8. JZOJ.5234【NOIP2017模拟8.7】外星人的路径
  9. SPOJ OPTM - Optimal Marks
  10. Minecraft Forge编程入门一 “环境搭建”