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. 【数据分析知识点】detailed table of contents

    Exploratory Data Analysis - Detailed Table of Contents http://www.itl.nist.gov/div898/handbook/eda/e ...

  2. linux 安装 Django 安装

    下载源码包:https://www.djangoproject.com/download/ 输入以下命令并安装: tar xzvf Django-X.Y.tar.gz # 解压下载包 cd Djang ...

  3. 【11-13】A股主要指数的市盈率(PE)估值高度

    全指材料(SH000987) - 2018-11-13日,当前值:12.4646,平均值:30.54,中位数:26.09865,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 内 ...

  4. goquery 文档

    https://www.itlipeng.cn/2017/04/25/goquery-%E6%96%87%E6%A1%A3/ http://blog.studygolang.com/2015/04/g ...

  5. EditDistance,求两个字符串最小编辑距离,动态规划

    问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...

  6. linq to xml 增删查改

    一.XML基本概述 XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境 ...

  7. MapWinGIS------引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常

    转载:http://www.cnblogs.com/dzone/archive/2011/09/08/2171445.html

  8. Mac OS 电信3G上网设置

    打开客户端后(安装客户端mobile partner需要先安装jdk),在“系统偏好设置”里选择“网络”,网络左侧添加“huaweimobile-modem”,“电话号码”填写电信卡号,“账户名称”和 ...

  9. 8 -- 深入使用Spring -- 4... Spring的AOP

    8.4 Spring的AOP AOP(Aspect Orient Programming),也就是面向切面编程,最为面向对象编程的一种补充. AOP和OOP互为补充,面向对象编程将程序分解成各个层次的 ...

  10. Linux Eclipse 运行Protobuf

    安装环境Ubuntu 14.04 64 bit 安装过程分为三步 1. Linux下安装Protobuf 2. Eclipse下安装protobuf.dt插件 3. Eclipse下配置动态链接库并运 ...