Level:

  Medium

题目描述:

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

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
], rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

思路分析:

  由于题目要求只能在原地进行旋转,那么我们不能使用额外的辅助空间,所以先进行matrix[i] [j]与matrix[j] [i]进行交换,然后matrix[i] [j]和matrix[i] [len-j]交换就能得到结果。

代码;

public class Solution{
public void rotate(int [][]matrix){
for(int i=0;i<matrix.length;i++){
for(int j=i;j<matrix[0].length;j++){
int temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length/2;j++){
int temp1=matrix[i][j];
matrix[i][j]=matrix[i][matrix[0].length-1-j];
matrix[i][matrix[0].length-1-j]=temp1;
}
}
}
}

30.Rotate Image(矩阵旋转)的更多相关文章

  1. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  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. leetcode48:矩阵旋转

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

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

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

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

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

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

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

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

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

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

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

  9. Java实现N*N矩阵旋转(360度)

    N*N矩阵旋转 Description 给你一个n*n的矩阵,你的任务是将它逆时针旋转角度d. [输入] 输入的第一个数为T,表示接下来有T组数据. 每组数据的格式如下: 第一行为两个整数n,d.1& ...

随机推荐

  1. 免费视频教学:30天精通iPho…

    原文地址:免费视频教学:30天精通iPhone手机编程(全)作者:苹果iphone软件编程 土豆连接http://www.tudou.com/playlist/id12638619.html

  2. 关于StringUtils类isEmpty、isNotEmpty、isBlank、isNotBlank针对null、空字符串和空白字符(如空格、制表符)的区别

    isEmpty | null | 空字符串("")|空白字符(空格.制表符)| | isEmpty | true | true | false | | isNotEmpty | f ...

  3. Linux实战教学笔记44:NoSQL数据库开篇之应用指南

    第1章 NoSQL数据库 1.1 NoSQL概述 自关系型数据库诞生40年以来,从理论产生发展到现实产品,例如:大家最常见的MySQL和Oracle,逐渐在数据库领域里上升到了霸主地位,形成每年高达数 ...

  4. 两个不同vim之间复制内容

    常规想法是打开两个vim,然后进行yy和p操作,但是实践证明根本是不行的.此时,我们需要分割窗口,然后就可以复制粘贴了.步骤如下: 假设我要把srv.c文件的readline函数整体复制到cli.c文 ...

  5. Color, Material, Lighting

    [Color, Material, Lighting] The material and lighting parameters are used to control the built-in ve ...

  6. 配置siebel捕捉SQL语句

    C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ...

  7. css实现栅格的方法

    1. 方法一 1.1. 效果 2. 方法二 2.1. 效果 3. 代码 3.1. Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  8. TCP端口状态说明ESTABLISHED、TIME_WAIT、 CLOSE_WAIT

    一. 首先说下tcp端口的几种状态: 1.LISTENING状态 FTP服务启动后首先处于侦听(LISTENING)状态. 2.ESTABLISHED状态 ESTABLISHED的意思是建立连接.表示 ...

  9. 最长的相同节点值路径 · Longest Univalue Path

    [抄题]: Given a binary tree, find the length of the longest path where each node in the path has the s ...

  10. vmware Selinux配置错误,导致无法启动虚拟机

    Linux 开机提示kernel panic - not syncing: Attempted to kill init! 解决方法: 系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜 ...