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. 05-Java中的String类

    程序设计思路: 首先目标是使输入的字符串加上某个数变成另一个字符串,从而相当于对字符串进行加密. 第一步输入一个字符串String类型: 第二步把这个字符串转变成字符数组: 第三步让这个数组的每一个字 ...

  2. python学习之——django环境搭建

    Django是一个基于MVC构造的框架,用于web开发,主要目的是简便.快速的开发数据库驱动的网站. 前提:已经安装python 搭建步骤: 1.https://www.djangoproject.c ...

  3. colorbox 自适应 高度

    $(".example3").colorbox({  inline: true, scrolling: false ,                    onComplete: ...

  4. agsXMPP参考代码

    agsXMPP 1.删除好友 XmppCon.RosterManager.RemoveRosterItem(node.RosterItem.Jid); 2.注销用户 void userConn_OnL ...

  5. apache结合svn创建svn资源库

    1.在登录过程中可以查看error日志,如果发生以下提示: (13)Permission denied: Could not open password file 2.运行:chcon -R -h - ...

  6. Mac下 Octave 中plot 无法绘制

    在coursera看机器学习课程的时候用到Octave来做数据处理,但是装了之后用plot画图时就会报错: set terminal aqua enhanced title "Figure ...

  7. mb_系列函数和普通字符函数的区别

    <?php //phpinfo(); $str = 'abcdef'; echo strlen($str);// 6 echo '<br/>'; echo substr($str, ...

  8. workflow createPath

    针对不同的流程,createpath不同,但是创建审批链,和创建表都有 1.GetUserInfoByListColumn 控件已创建的.先Rebuild,属性需要注意 2.CreateListIte ...

  9. Cracking-- 17.13 将二叉树转换成双向链表

    在书的105页 使用中根遍历的思想 left 之后 为 root 之后 为 right 则对左子树来说 left->right = root; root->left = left; 对右子 ...

  10. 写了一个基于React+Redux的仿Github进度条

    曾经实现过Angular版,这次感觉用了高大上的React却写了更多的代码,需要的配置也更多了,有利有弊吧. 但这个“导航条问题”很有意思,涉及到在Redux中写timer,其实我很困惑,到底如何完美 ...