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]
]
class Solution {
public void rotate(int[][] matrix) {
int len = matrix.length; // rotate diagnal first
for (int i = 0; i < len; i++) {
for (int j = 0; j < i; j++) {
swap(matrix, i, j, j, i);
}
} // rotate vertical next
for (int i = 0; i < len; i++) {
for (int j = 0; j < len/2; j++) {
swap(matrix, i, j, i, len - 1 - j);
}
}
} private void swap(int[][] matrix, int a, int b, int c, int d) {
int tmp = matrix[a][b];
matrix[a][b] = matrix[c][d];
matrix[c][d] = tmp;
}
}

[LC] 48. Rotate Image的更多相关文章

  1. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  2. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  3. 11/8 <matrix> LC 48 54 59

    48. Rotate Image 先按对角线对称图形,再水平对折. class Solution { public void rotate(int[][] matrix) { //1.transpos ...

  4. 刷题48. Rotate Image

    一.题目说明 题目是48. Rotate Image,简而言之就是矩阵顺时针旋转90度.不允许使用额外的矩阵. 经过观察(写一个矩阵,多看几遍就知道了),旋转90度后: 第1行变为len-1列(最后一 ...

  5. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

  6. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. [leetcode 48] rotate image

    1 题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwi ...

  8. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  9. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

随机推荐

  1. uniapp 初始化项目

    const baseUrl = 'http://10.92.1.17:6601/videoapi/'; //const baseUrl = '/videoapi/'; //对于 GET 方法,会将数据 ...

  2. MySQL--索引和外键

    来自:http://www.jb51.net/article/32149.htm 1.添加PRIMARY KEY(主键索引)  ALTER TABLE `table_name` ADD PRIMARY ...

  3. JavaSE--for each

    参考:http://blog.csdn.net/yasi_xi/article/details/25482173 学习多线程的时候实例化线程数组而挖掘出来的一直以来的理解误区 之前一直以为for ea ...

  4. Java 静态static关键字,main函数,对象的初始化过程,对象调用成员,单例模式的设计,静态代码块(6)

    Java 静态static关键字,静态代码块详情参考:static的使用原理讲解http://www.cnblogs.com/itcqx/p/5519464.html main函数: java Mai ...

  5. JDK11 JAVA11下载安装与快速配置环境变量教程

    https://blog.csdn.net/weixin_40928253/article/details/83590136 1.到Oracle官网下载jdk11,并安装.搜索“jdk",选 ...

  6. idHttpServer接收类型

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. UVA 10806 最小费用最大流

    终于可以写这道题的题解了,昨天下午纠结我一下下午,晚上才照着人家的题解敲出来,今天上午又干坐着想了两个小时,才弄明白这个问题. 题意很简单,给出一个无向图,要求从1 到 n最短路两次,但是两次不允许经 ...

  8. matlab画图中的坐标轴设置

    ax = gca; ax是个结构体,查看ax变量,可以看到所有可设置的属性.几个常见属性如下: 设置坐标轴字体大小,粗细,字体名 2014b之后版本: ax = gca; ax.FontSize = ...

  9. Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

    Necklace HDU - 3874  Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...

  10. servlet 3 通过编程的方式来配置ServletContext

    你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...