1 题目

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?

2 思路

使用辅助空间比较简单。

不使用辅助空间,想了半天,没想出来。。

看别人的代码,原来是要翻转两次。。

3 代码

    public void rotate(int[][] matrix) {
// 使用多余空间的
int length = matrix.length;
Integer[][] m = new Integer[length][length];
for(int i=;i<length;i++){
for(int j = ;j<length;j++){
m[j][length-i-] = matrix[i][j];
}
}
for(int i=;i<length;i++){
for(int j = ;j<length;j++){
matrix[i][j] = m[i][j];
}
}
} // 不使用多余空间 // 法1: exchange up and down matrix[i][j] = matrix[length-1-i][j],swap symmetry matrix[i][j] = matrix[j][i]
public void rotate1(int[][] matrix) {
int length = matrix.length;
for(int i=;i<length/;i++){
for(int j = ;j<length;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[length--i][j];
matrix[length--i][j] = temp;
}
} for(int i=;i<length;i++){
for(int j = ;j<i;j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
} // 法2: transpose(matrix[i][j]=matrix[j][i], flip horizontally matrix[i][j] = matrix[i][length-1-j]

[leetcode 48] rotate image的更多相关文章

  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. [LeetCode] 48. 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(旋转图像)

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

  4. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

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

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

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

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

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

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

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

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

  9. 【刷题笔记】LeetCode 48. Rotate Image

    题意 原地顺时针翻转一个 n*n 的矩阵 图解 下面例子中用 5*5 矩阵做示例,如下图,我们要把该矩阵顺时针翻转90度,并且不能使用另外的矩阵空间来暂存数据,而是原地改变矩阵中数值. 我的想法是这样 ...

随机推荐

  1. php获取数据库中数据,转成json数据

    <?php //需要执行的SQL语句 //单条 $sql="select * from xinwen"; //多条数据 //$sql="select id,name ...

  2. MacDev.Mach-O.Programming-Part-III:MachOView-v2.4.9200.dmg-crash

    MachOView-v2.4.9200.dmg Crash 在OS X(其版本号: 10.11.6 (15G31))下载MachOView-2.4.9200.dmg后,打开Fat Binary后,Ma ...

  3. IplImage 结构解读

    IplImage 结构解读: typedef struct _IplImage { int nSize;                              /* IplImage大小,等于wi ...

  4. Windows cmd命令搜索顺序

    一.在cmd中执行一个不带后缀的命令(不带路径),首先会在无后缀的系统命令(如cd.dir等)中搜索,如果找到了就执行该命令, (dir是无后缀的系统命令所以优先执行,无视当前目录中的dir.exe) ...

  5. mysql主从不一致解决方法

    方法一:忽略错误后,继续同步 该方法适用于主从库数据相差不大,或者要求数据可以不完全统一的情况,数据要求不严格的情况 stop slave; #表示跳过一步错误,后面的数字可变 set global ...

  6. 下载Spring框架开发包

    1.打开官网: http://spring.io/,打开project >> spring framework 2.在右侧找到要用的版本,如4.3.4,打开reference,搜索&quo ...

  7. Apache 网站301重定向

    1. Apache模块 开启rewrite 2..htaccess文件中 <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteE ...

  8. labview学习_入门篇(一)

    写在前面的话: 在上大学的时候,实验室的老师推荐用labview工具编写上位机软件,当时不想用labview,感觉不写代码心里不踏实,后来用vb和matalb开发了上位机软件.但现在由于部门的几款工具 ...

  9. 如何用Matlab将cell数据写入文件

    我们知道,一般的文件读写函数是不接受直接将cell内容(非数值)直接写入文件的, 例如:dlmwrite('o.txt', C, 'delimiter', '\t');%C 为cell类型数据,会报错 ...

  10. Unity加载模块深度解析(纹理篇)

    在游戏和VR项目的研发过程中,加载模块所带来的效率开销和内存占用(即“加载效率”.“场景切换速度”等)经常是开发团队非常头疼的问题,它不仅包括资源的加载耗时,同时也包含场景物件的实例化和资源卸载等.在 ...