[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两部分. 要求如下: 圣剑的所有护符, ...
随机推荐
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- lufylegend库 LButton
lufylegend库 LButton <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- loadrunner:从数据库中取值进行参数化
下面我们介绍用数据库中的用户名来参数化登陆用户名. 框选住登陆名,点鼠标右键,弹出对话框,选择"替换为新参数"弹出对话框,此时参数名输入:name,参数类型选择File,如图 点& ...
- JavaScript 正则表达式入门教程
正则表达式是描述一组字符串特征的模式,用来匹配特定的字符串 主要分三个部分:基本语法.RegExp对象的方法.JS中支持正则表达式的String对象方法 一.基本语法 在JS中,正则表达式为对象,用如 ...
- DEVTMPFS
devtmpfs选项保证了系统启动使用临时文件系统.不选会启动出现readonly ,无法启动
- 【ci框架基础】之部署百度编辑器
在ci框架下加载编辑器,现在复习下内容.我的框架文件名称为ci 1.下载百度编辑器ueditor,http://ueditor.baidu.com/ 一般情况下下载ubuilder版即可,并将uedi ...
- 第七届蓝桥杯javaB组真题解析-凑算式(第三题)
题目 /* 凑算式 B DEF A + --- + ------- = 10 C GHI (如果显示有问题,可以参见[图1.jpg]) 这个算式中A~I代表1~9的数字,不同的字母代表不同的数字. 比 ...
- IPv4地址学习总结
一. IPv4地址格式 网络互连的一个重要前提条件是要有一个有效的地址结构,并且所有的互连网络用户都应遵守这个地址结构.因为只有这样所有的互连网络用户才能在统一的规定下相互之间通讯.这个地址结构可以有 ...
- ArcGIS制图表达Representation-规则和几何效果
ArcGIS制图表达Representation-规则和几何效果 by 李远祥 对制图表达运用是否纯熟,主要是看对制图表达规则的理解和巧妙运用.前面章节已经粗略介绍过制图表达的运用,通过一些简单的规则 ...
- QML Object Attributes QML对象属性
QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...