Problem:给定一个n*n的二维图片,将这个二维图片按照顺时针旋转90°
 
求解过程:
 
 
求解过程如下所示:
 
 

首先对矩阵进行按照对角线进行交换操作
 
之后将矩阵前n/2列与后n/2列进行两两交换操作 (0<—>n-1     1<—>n-2    2<—>n-3) 
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 实现二维图片顺时针旋转90度
*/
public class Solution48 {
public static void rotate(int[][] matrix) {
int len = matrix.length;
for(int i = 0; i < len; i++){
for(int j = i+1; j < len; j++){
swap(matrix,i,j,j,i);
}
}
for(int i = 0 ; i < len ; i++){
for(int j = 0 ; j < len / 2; j++){
swap(matrix,i,j,i,len-1-j);
}
}
} private static void swap(int[][] matrix, int i1,int j1, int i2,int j2) {
int temp = matrix[i1][j1];
matrix[i1][j1]=matrix[i2][j2];
matrix[i2][j2]=temp;
} public static void main(String[]args){
int [][]matrix={{1,2,3},{4,5,6},{7,8,9}};
rotate(matrix);
int len = matrix.length;
for(int i = 0 ; i < len; i++){
for(int j = 0 ; j < len; j++){
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
}
 

LeetCode 48 Rotate Image(2D图像旋转问题)的更多相关文章

  1. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  2. 每日算法37:Rotate Image (图像旋转)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. LeetCode 48. Rotate Image My Submissions Question (矩阵旋转)

    题目大意:给一个矩阵,将其按顺时针旋转90°. 题目分析:通法是先将矩阵转置,然后再反转每一行,或者是先反转每一列,然后再将其转置.I just want to say"It's amazi ...

  4. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode 48. Rotate Image(旋转图像)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  7. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  8. [leetcode]48. Rotate Image旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. LeetCode 48. Rotate Image (C++)

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

随机推荐

  1. C# 之HTTP请求get,post带重试参数

    public class WebHttp { /// <summary> /// get请求带重试 /// </summary> /// <param name=&quo ...

  2. 删除mac系统win10启动选择项

    打开终端输入:diskutil list找到EFI这个分区,挂载EFI分区diskutil mount /dev/disk0s1 回到Finder 删除除apple之外的两个文件夹就可以了(删除win ...

  3. Service 保活法之一

    我们都知道,在Android中,Service有两种启动方式: startService 以startService()启动服务,系统将通过传入的Intent在底层搜索相关符合Intent里面信息的s ...

  4. Android检查设备是否可以访问互联网,判断Internet连接,测试网络请求,解析域名

    安卓SDK提供了ConnectivityManager类,那么我们就可以轻松的获取设备的网络状态以及联网方式等信息. 但是要想知道安卓设备连接的网络能不能访问到Internet,就要费一番周折了. 本 ...

  5. 按键精灵如何调用Excel及按键精灵写入Excel数据的方法教程---入门自动操作表格

    首先来建立一个新的Excel文档,在桌面上点击右键,选择[新建]-[Excel工作表],命名为[新手学员]. 现在这个新Excel文档是空白的,我们接下来会通过按键精灵的脚本来打开并写入一些数据.打开 ...

  6. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

  7. python 读取csv文件

    python中有一个读写csv文件的包,直接import csv即可 新建test.csv 1.写 import csv with open("test.csv","w& ...

  8. 如何通过Node.js启动cesium

    设置一个Web服务器通过Node.js是很容易的,只需要3个步骤: (1)从安装Node.js网站,你可以使用默认安装设置. (2)打开命令行,然后进入Cesium的根目录,通过npm install ...

  9. mysql中json_remove函数的使用?

    需求描述: 今天看json记录,可以通过json_remove函数对一个key或多个key从个json记录中去掉. 操作过程: 1.查看一个已经存在的json表 mysql> select * ...

  10. js获取iframe里面的元素

    直接获取不行 var  win2 = document.querySelector('iframe[width = "1280" ]').contentWindow; var lo ...