Rotate Image
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?
开始做的时候使用了额外的matrix同等大小的空间,不属于inplace 的方案,通过看discuss, 看到如下通过两步可以做到inplace 的方案顺时针旋转90度,代码如下:
public class Solution {//inplace solution
public void rotate(int[][] matrix) {
int n=matrix.length;
int mid=n/2;
//上下对折交换
//1,2,3 7,8,9
//4,5,6 ——> 4,5,6
//7,8,9 1,2,3
for(int i=0;i<mid;i++)
{
for(int j=0;j<n;j++)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[n-1-i][j];
matrix[n-1-i][j]=temp;
}
}
//按对角线交换
//7,8,9 7,4,1
//4,5,6 ——> 8,5,2
//1,2,3 9,6,3
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
}
}
Rotate Image的更多相关文章
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- jQuery.rotate.js参数
CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate) 在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...
- 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例
<!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...
- 记一道有意思的算法题Rotate Image(旋转图像)
题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an ...
- iOS 2D绘图 (Quartz2D)之Transform(CTM,Translate,Rotate,scale)
前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation ...
随机推荐
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- IBatis 批量插入数据之SqlBulkCopy
public void AddLetters(IList<int> customerIds, string title, string content, LetterEnum.Letter ...
- Android ViewPager sharedpreferences
http://www.cnblogs.com/dwinter/archive/2012/02/27/AndroidViewPager%E5%A4%9A%E9%A1%B5%E9%9D%A2%E6%BB% ...
- codevs2572 路面修整
题目描述 Description Mr. Ling打算好好修一下学校门口的那条凹凸不平的路.按照Mr. Ling的设想,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同 ...
- javascript 函数与对象
javascript中的函数是非常重要的概念,也是比较难于理解的一个知识点! 下面就来聊聊函数: JS基于对象:什么是基于对象呢?简单的说所有代码都是"对象"; 比如函数: fun ...
- uboot学习第一天
Windows操作系统BIOS(设置) Windows系统 文件系统 驱动程序 应用程序 linux操作系统bootloader(引导系统) kernel(内核) 文件系统 驱动程序 应用程序 交叉编 ...
- JavaScript -- 小试牛刀
//var a = parseInt(window.prompt("请输入一个数字!","")); //switch(a) { // case 1 : // c ...
- 微信电脑版-微信for windows客户端发布
12月份微信Windows版客户端1.0 Alpha推出,昨天微信for windows 1.0客户端(测试版)发布更新,超过三亿人使用的聊天应用,现在登录Windows桌面.你可以在Windows上 ...
- PHP中的变量与常量详解
几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...
- C和指针 第三章 变量的储存类型 auto、static、register以及static关键词
变量的储存类型决定标量何时创建,何时销毁以及他的值保持多久.有三个地方可以储存变量: 普通内存static 运行时堆栈auto 硬件寄存器register 变量的缺省储存类型取决于它的声明位置: 静态 ...