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. Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统

    最近正在学习Vue2.0相关知识,正好近期饿了么桌面端组件Element-UI发布,便动手做了一款简易个人记账系统,以达到实践及巩固目的. 1.开发环境 Win10 + VS2015 + Sqlser ...

  2. 【随笔】Linux服务器备份相关

    服务器数据的安全性一直都是服务器日常管理的重中之重.Linux服务器虚拟化虽然以其高度可靠的作业系统而闻名,不过系统失效仍然可能发生.可能因为硬体故障,电源中断,或其他不可预料的问题.更常见的这 些问 ...

  3. workflow GetListIdByName 获取表名

    1.Assign   获取表的地址 和表名 2.HttpsendWithSuspend==HttpSend 3.ParseDynamicValue 4.GetDynamicValuePropertie ...

  4. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  5. BLE资料应用笔记 -- 持续更新

    BLE资料应用笔记 -- 持续更新 BLE 应用笔记 小书匠 简而言之,蓝牙无处不在,易于使用,低耗能和低使用成本.'让我们'更深入地探索这些方面吧. 蓝牙无处不在-,您可以在几乎每一台电话.笔记本电 ...

  6. JS组件系列——封装自己的JS组件

    前言:之前分享了那么多bootstrap组件的使用经验,这篇博主打算研究下JS组件的扩展和封装,我们来感受下JQuery为我们提供$.Extend的神奇,看看我们怎么自定义自己的组件,比如我们想扩展一 ...

  7. 按Enter键触发事件

    1.document.onkeydown=function(e){        var keycode=document.all?event.keyCode:e.which;        if(k ...

  8. UVA11149_Power of Matrix

    题目简洁明了,给出矩阵,求前k次方和. 不知道这种方法是叫做二分幂还是倍增法,如果有知道的,请告诉我一下. 具体思想是这样的,A^1+A^2+A^3+......A^n=(E+A^(n/2))*(A^ ...

  9. 发现的eval的一个小问题

    首先我们来看五段代码: 第一段代码: function test(){ eval('var a = 1;'); alert(a); } test(); 第二段代码: function test(){ ...

  10. 论ubuntu的作死技巧

    此处记录自己弄崩系统的几大杀器,长期更新. 1. sudo apt-get autoremove