[LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image
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(矩阵旋转)的更多相关文章
- 【LeetCode】【矩阵旋转】Rotate Image
描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- 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 ...
- leetcode 48 矩阵旋转可以这么简单
一行代码解决矩阵旋转(方法三). 方法1: 坐标法 def rotate(self, matrix): n = len(matrix) # 求出矩阵长度 m = (n + 1) // 2 # 求出层数 ...
- leetcode48:矩阵旋转
题目链接 输入一个N×N的方阵,要求不开辟新空间,实现矩阵旋转. 将点(x,y)绕原点顺时针旋转90度,变为(y,-x).原来的(-y,x)变为(x,y) class Solution(object) ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和
题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将 ...
- c++刷题(43/100)矩阵旋转打印
题目1:矩阵旋转打印 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则 ...
- 利用neon技术对矩阵旋转进行加速
一般的矩阵旋转操作都是对矩阵中的元素逐个操作,假设矩阵大小为m*n,那么时间复杂度就是o(mn).如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素,对多个元素进行操作,虽然时间复杂 ...
- 洛谷P3933 Chtholly Nota Seniorious 【二分 + 贪心 + 矩阵旋转】
威廉需要调整圣剑的状态,因此他将瑟尼欧尼斯拆分护符,组成了一个nnn行mmm列的矩阵. 每一个护符都有自己的魔力值.现在为了测试圣剑,你需要将这些护符分成 A,B两部分. 要求如下: 圣剑的所有护符, ...
随机推荐
- JS base64 加密和解密
/*** * 加密 base64encode(utf16to8(str)) * 解密 utf8to16(base64decode(str)) * * */ var base64EncodeChars ...
- EntityFramework Core解决并发详解
前言 对过年已经无感,不过还是有很多闲暇时间来学学东西,这一点是极好的,好了,本节我们来讲讲EntityFramewoek Core中的并发问题. 话题(EntityFramework Core并发) ...
- Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析
转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...
- MySql学习(七) —— 查询性能优化 深入理解MySql如何执行查询
本篇深入了解查询优化和服务器的内部机制,了解MySql如何执行特定查询,从中也可以知道如何更改查询执行计划,当我们深入理解MySql如何真正地执行查询,明白高效和低效的真正含义,在实际应用中就能扬长避 ...
- springmvc注解式事务手动回滚
Spring的AOP事务管理默认是针对unchecked exception回滚(运行期异常,Runtime Exception),如果希望手动控制事务的回滚,可以通过 TransactionAspe ...
- java 继承的学习(转)
转自:http://www.cnblogs.com/happyframework/p/3332243.html,非常感谢啊 public class test { /** * @param args ...
- 如何一秒钟从头构建一个 ASP.NET Core 中间件
前言 其实地上本没有路,走的人多了,也便成了路. -- 鲁迅 就像上面鲁迅说的那样,其实在我们开发中间件的过程中,微软并没有制定一些策略或者文档来约束你如何编写一个中间件程序, 但是其中却存在者一些最 ...
- Vue2.0源码阅读笔记--双向绑定实现原理
上一篇 文章 了解了Vue.js的生命周期.这篇分析Observe Data过程,了解Vue.js的双向数据绑定实现原理. 一.实现双向绑定的做法 前端MVVM最令人激动的就是双向绑定机制了,实现双向 ...
- dev简单实现柱状图,曲线图
1.数据源代码: DataTable dt = new DataTable(); dt.Columns.Add("A"); dt.Columns.Add("B" ...
- java中读取特殊文件的类型
java中读取特殊文件的类型: 第一种方法(字符拼接读取): public static String getType(String s){ String s1=s.substring(s.index ...