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. js原生设计模式——3简单工厂模式\js面向对象编程实例

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  2. java IMAGEIO

    javax.imageio使用 ImageIO 类的静态方法可以执行许多常见的图像 I/O 操作. 此包包含一些基本类和接口,有的用来描述图像文件内容(包括元数据和缩略图)(IIOImage): 有的 ...

  3. Bootstrap入门(九)组件3:按钮组

    Bootstrap入门(九)组件3:按钮组   先引入本地的CSS文件和JS文件(注:1.bootstrap是需要jQuery支持的.2.需要在<body>当中添加) <link h ...

  4. We Chall-Training: Crypto - Caesar I-Writeup

    MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...

  5. Ionic start 创建项目报错 Error with start undefined

    转自:http://blog.csdn.net/wenzigui_qy/article/details/52874542 在Installing npm packages的时候报错,如下: Insta ...

  6. App开发外包必须注意的四大骗局

    在app外包过程中有很多需要注意的事项,今天专门挑选注意事项中的"骗局"这个话题来与大家分享一些的常见骗局及其细节. 无论是从新闻还是身边的朋友,我们都经常可以听见"xx ...

  7. .NET Core在WindowsServer服务器部署及发布

    VS使用WEB DEPLOY发布.NET Core程序   背景是这样的,公司有两台服务器,平时一台备用,另一台做为主生产机器.当有大量补丁或者安装什么东西需要重启的时候,交其中一台直接关掉IIS,然 ...

  8. java Socket(TCP)编程小项目

    package 服务器端相关操作; import java.io.Serializable; /* * 创建存储需要传输信息的对象,方便客户端向服务器端传送数据 */ public class Cli ...

  9. Windows 10 IoT Serials 6 - 如何修改IoTStartupOnBoot.cmd文件

    使用Windows 10 IoT Core系统的朋友应该会比较熟悉IoTStartupOnBoot.cmd文件,该文件是系统启动以后加载的批处理文件,一般会包含应用.服务和后台的启动,比如WinRM. ...

  10. poj1611

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35918   Accepted: 17458 De ...