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. Obj格式模型 读取

    OBJ文件的结构 在一个OBJ文件中,首先有一些以v.vt或vn前缀开头的行指定了所有的顶点.纹理坐标.法线的坐标.然后再由一些以f开头的行指定每一个三角形所对应的顶点.纹理坐标和法线的索引.在顶点. ...

  2. Maven初步踩坑

    2015-02-08 今天创建maven项目,要从中央仓库下载一堆包到本地仓库,等了好久.结果下好了之后,maven项目上有个感叹号,也没有发现代码里哪配置有错误. 和实验室好多小伙伴一起交流 也没找 ...

  3. asp.net操作cookie类,包含datatable批量存入cookie

    以下是类: public class CookieMgr { #region 快速储存Cookie /// <summary> /// 快速储存Cookie /// </summar ...

  4. [Bayesian] “我是bayesian我怕谁”系列 - Exact Inference

    要整理这部分内容,一开始我是拒绝的.欣赏贝叶斯的人本就不多,这部分过后恐怕就要成为“从入门到放弃”系列. 但,这部分是基础,不管是Professor Daphne Koller,还是统计学习经典,都有 ...

  5. WebBrowser中html元素如何触发winform事件

    这个问题来自论坛提问,对dom稍微了解的话还是比较简单的,只要注册一下事件就可以了. C#代码如下: using System;using System.ComponentModel;using Sy ...

  6. c#系统消息类封装

    今天封装了一个返回json的消息类 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  7. iscroll5实现下拉加载更多

    1 下载最新的iscroll5,本文版本是5.1.3 2 提取iscroll-probe.js,选择这个文件的原因是我们要给iscroll扩展一个事件,需要用到probeType 属性 3 修改isc ...

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

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

  9. c 各种编译器(gcc clang)

    很多时候,出现一些类似GNU,GCC,CLANG,LLVM等与编译器有关的名词的时候,都不太清楚它到底是干嘛的,理解这些东西后, 对于xcode中很多配置型的需求修改起来都会得心应手,因此有必要了解透 ...

  10. pycharm 激活

    方法1: (1)更新**hosts**文件 [hosts文件百度云下载地址](https://pan.baidu.com/s/1o9ZujxS) **hosts**文件在windows中的地址为: C ...