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. 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)

    15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...

  2. 問題排查:类型“System.DateTime”的对象无法转换为类型“System.String”

    最近在擴充資料對接工具的功能 經常會遇到這個狀況 當然還有其他同類提示,例如 int/decimal 無法轉 System.String 等等 無獨有偶 這些錯誤幾乎都是在 DataTable 轉換成 ...

  3. 解决Visual Studio 2010闪退问题

    许多人都会面临这样的问题,vs2010闪退,明明程序执行成功,明明没有错误,缺闪了一下结束. 闪退问题主要是缓存的问题,通过在程序末尾,即main函数后增加 getchar(); getchar(); ...

  4. 获取当前运行dll文件的路径

    char moduledir[MAX_PATH];  GetModuleFileNameA(GetModuleHandleA("ppdl_BE081_BIW_seal_library.dll ...

  5. 向NFV过渡 这三个坑我替你趟了

    网络功能虚拟化(NFV)在最近几年不断发展,并开始现实商业部署.通信服务商也必须解决自身所面临的挑战,包括安全.性能.投入产出比.与现有网络实现电信级交互.运营支撑系统(OSS)和业务支撑系统(BSS ...

  6. 纯脚本组装Json格式字符串

    var answerStr = "["; for (var i in answer) { var data = $("input[name=QuestionItem_&q ...

  7. chrome浏览器插件的开启快捷键

    用鼠标去打开chrome浏览器右上角的插件,总是感觉太麻烦,例如你想用有道词典的插件查一个单词的意思,用鼠标把有道插件打开,然后再回到键盘上敲单词,真的好麻烦.现在只要设置一下插件的快捷键就OK了. ...

  8. SVN服务器的配置(简单易懂,带配置文件,有注释)

    这两天在服务器搭建了一个SVN服务器,一些经验,也留作后用把,有不详细的欢迎批评指正 另外关于子目录的访问配置,这块我还是不懂,希望有前辈能教我一下 1.安装SVN Serveryum install ...

  9. jsp去掉小数点

    <fmt:formatNumber value="${zyUser.user_gold}" pattern="0"/>

  10. javaWeb实现文件上传与下载 (转)

    文件上传概述 实现web开发中的文件上传功能,需完成如下二步操作: 在web页面中添加上传输入项 在servlet中读取上传文件的数据,并保存到本地硬盘中. 如何在web页面中添加上传输入项? < ...