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原生设计模式——9外观模式封装2(小型代码库YJ)

    <script type="text/javascript">    //小型代码库YJ封装    var YJ = {        //根据id获取元素       ...

  2. js变量作用域及访问权限的探讨(2)

    每一种语言都有变量的概念,变量是用来存储信息的一个元素.比如下面这个函数: 复制代码 代码如下:  function Student(name,age,from)  {   this.name = n ...

  3. Extjs换肤+cookie皮肤记忆功能

    http://www.myext.cn/kaifa/a_102.html    Ext之家 <title>无标题页</title>    <link rel=" ...

  4. 将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法

    将[4,3,2,5,4,3]分割成[4,3,2].[5,4,3]两个List的算法 package com.srie.test; import java.util.ArrayList; import ...

  5. DNS信息

    主机A记录: 描述主机地址记录,在dns域名和ip地址之间建立映射关系语法: owner class ttl A IP_v4_address eg: host1.example.mircrosoft. ...

  6. jQuery插入节点(移动节点)

    jQuery插入节点(移动节点) <%@ page language="java" import="java.util.*" pageEncoding=& ...

  7. 如何在Crystal框架项目中内置启动Zookeeper服务?

    当Crystal框架项目需要使用到Zookeeper服务时(如使用Dubbo RPC时,需要注册服务到Zookeeper),而独立部署和启动Zookeeper服务不仅繁琐,也容易出现错误. 在小型项目 ...

  8. Mac下安装cscope和ctags

    Mac下默认没有cscope和ctags,太不爽了,还好可以自己编译一个放进来 一.下载地址 cscope: http://downloads.sourceforge.net/project/csco ...

  9. ssh配置文件及问题解决

    一 ssh的配置文鉴模板 Host AAA User gitolite3 HostName IP地址 IdentityFile ~/.ssh/key 二 下载代码的方法 1 ssh git2 git ...

  10. [html5] 学习笔记-bootstrap介绍

    1.Bootstrap介绍 Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 2.下面对于官网上给出的最简单的一个bootstra ...