48. Rotate Image

 
 
Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

显然的,矩阵旋转。

这里我是多开一个数组来直接赋值,没有原地翻转。

或许原地翻转能更快。

package leetcode.RotateImage;

public class Solution {

    public void rotate( int[][] matrix ) {
int[][] newMatrix = new int[ matrix.length ][ matrix[ 0 ].length ];
int sX = 0;
int sY = 0;
int eX = matrix.length - 1;
int eY = matrix[ 0 ].length - 1;
while( sX <= eX && sY <= eY ) {
int sx = sX;
int sy = sY;
int ex = eX;
int ey = eY;
roteUpToRight( matrix, newMatrix, sx, sy, ey );
roteRightToBottom( matrix, newMatrix, sx, sy, ex, ey );
roteBottomToLeft( matrix, newMatrix, sx, sy, ex, ey );
roteLeftToUp( matrix, newMatrix, sx, sy, ex, ey );
sX++;
sY++;
eX--;
eY--;
}
for( int i = 0; i < matrix.length; i++ ) {
for( int j = 0; j < matrix[ 0 ].length; j++ ) {
matrix[ i ][ j ] = newMatrix[ i ][ j ];
}
}
} private void roteLeftToUp( int[][] matrix, int[][] newMatrix, int sx, int sy, int ex, int ey ) {
for( int i = sy,r=0; i < ey; i++,r++ ) {
newMatrix[ sx ][ i ] = matrix[ ex - r ][ sx ];
}
} private void roteBottomToLeft( int[][] matrix, int[][] newMatrix, int sx, int sy, int ex, int ey ) {
for( int i = sx; i < ex; i++ ) {
newMatrix[ i ][ sy ] = matrix[ ex ][ i ];
}
} private void roteRightToBottom( int[][] matrix, int[][] newMatrix, int sx, int sy, int ex, int ey ) {
for( int i = sy,r=0; i <= ey; i++,r++ ) {
newMatrix[ ex ][ i ] = matrix[ ey-r ][ ey ];
}
} private void roteUpToRight( int[][] matrix, int[][] newMatrix, int sx, int sy, int ey ) {
for( int i = sy; i <= ey; i++ ) {
newMatrix[ i ][ ey ] = matrix[ sx ][ i ];
}
} public static void main( String[] args ) {
//int[][] matrix = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
// int[][] matrix = new int[][]{{6,7},{9,10}};
int[][] matrix = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
Solution s = new Solution();
s.rotate( matrix );
} }

[LeetCode]Rotate Image(矩阵旋转)的更多相关文章

  1. 【LeetCode】【矩阵旋转】Rotate Image

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

  2. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  3. leetcode 48 矩阵旋转可以这么简单

    一行代码解决矩阵旋转(方法三). 方法1: 坐标法 def rotate(self, matrix): n = len(matrix) # 求出矩阵长度 m = (n + 1) // 2 # 求出层数 ...

  4. leetcode48:矩阵旋转

    题目链接 输入一个N×N的方阵,要求不开辟新空间,实现矩阵旋转. 将点(x,y)绕原点顺时针旋转90度,变为(y,-x).原来的(-y,x)变为(x,y) class Solution(object) ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

    题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...

  7. c++刷题(43/100)矩阵旋转打印

    题目1:矩阵旋转打印 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则 ...

  8. 利用neon技术对矩阵旋转进行加速

    一般的矩阵旋转操作都是对矩阵中的元素逐个操作,假设矩阵大小为m*n,那么时间复杂度就是o(mn).如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素,对多个元素进行操作,虽然时间复杂 ...

  9. 洛谷P3933 Chtholly Nota Seniorious 【二分 + 贪心 + 矩阵旋转】

    威廉需要调整圣剑的状态,因此他将瑟尼欧尼斯拆分护符,组成了一个nnn行mmm列的矩阵. 每一个护符都有自己的魔力值.现在为了测试圣剑,你需要将这些护符分成 A,B两部分. 要求如下: 圣剑的所有护符, ...

随机推荐

  1. PHP的数组值传入JavaScript的数组里

    <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html><head>       &l ...

  2. HTML5学习之语义化标签(一)

    一.为什么HTML5要引入新语义标签 在HTML5出现之前,我们一般采用DIV+CSS布局我们的页面.但是这样的布局方式不仅使我们的文档结构不够清晰,而且不利于搜索引擎爬虫对我们页面的爬取.为了解决上 ...

  3. IOS网络请求之NSURLSession使用

    前言: 无论是Android还是ios都离不开与服务器交互,这就必须用到网络请求,记得在2013年做iOS的时候那时候用的ASIHTTPRequest框架,现在重新捡起iOS的时候ASIHTTPReq ...

  4. cordova StatusBar插件的使用(设置手机状态栏颜色和页面头部颜色一致),做出和原生一样的页面效果体验

    cordova StatusBar插件的使用(设置手机状态栏颜色和页面头部颜色一致),做出和原生一样的页面效果体验设置设备状态栏背景颜色StatusBar.backgroundColorByHexSt ...

  5. Linux驱动技术(四) _异步通知技术

    异步通知的全称是"信号驱动的异步IO",通过"信号"的方式,放期望获取的资源可用时,驱动会主动通知指定的应用程序,和应用层的"信号"相对应, ...

  6. Linq 集合操作

    Linq 集合操作 演示代码 两个对象一个是Person,一个Address, AddressId是外键, public class Person { public string ID { get; ...

  7. C语言 一维数组叠加为二维数组样例

    这里参看memcpy的用法,将一个一维整型数组不停的叠加为二维数组 使用宏定义来控制二维数组的行列 代码如下: #include <stdio.h> #include <stdlib ...

  8. ASP.NET应用程序的文件类型及文件夹列表

    文件类型: 1. *.aspx文件:这类文件是ASP.NET Web页面,它包括用户接口和隐藏代码. 2. *.ascx文件:这类文件是用户控件.用户控件同Web页面非常相似,但用户不能直接访问用户控 ...

  9. Swift3.0 中 Strings/Characters 闲聊

    前言 本篇文章主要浅析字符串\字符在 Swift 和 Objective-C 之间的区别及其简单用法.如有不妥的地方还望大家及时帮忙纠正. 字符串判空 在 swift 语言中空字符串初始化方式常用的有 ...

  10. Angularjs学习笔记《一》

    开始慢慢的学习新的框架,Angularjs,其中原理不知深浅,但有例子练习,慢慢熟知.看的英文文档一点点翻译学习. 第一个例子,写一个增加,选中,删除的例子, 首先要引进所用的框架源码,这样引进,下面 ...