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. ruoyi LogUtils

    package com.ruoyi.framework.util; import java.io.PrintWriter; import java.io.StringWriter; import ja ...

  2. English Grammar - Subject Clause

    that引导主语从句 一般置于句末,偶尔也置于句首 that引导的主语从句置于句首 That the seas are being overfished has been known for year ...

  3. 关于luoguU67856 数列一题

    本题采用累加法 首先这个式子\[a_n = ka_{n-1}+b\]的通项不用我说了吧 然后就是累加法 \[S_n = \sum_{i=1}^{n} a_i = \sum_{i=1}^{n} ka_{ ...

  4. Graph & Tree

    图论学习笔记 TYQ图论真是个渣渣呢 所以TYQ决定猛补图论 好的从0x60开始 表示博客园不用Latex真的烦呢QAQ,公式难打的要命QAQ 0x60~0x62 最短路讲解跳过 最小生成树: Kru ...

  5. Matlab高级教程_第四篇:Matlab高级函数_关键词:arrayfun

    % 定义一个句柄并用这个句柄求值 h = @sin; h(3) % % 命令窗口返回内容 % h(3) % ans = % 0.1411 %定义一个完整句柄,@参数+表达式的形式,并给句柄传参 h1 ...

  6. dubbo的灰度发布

    1,什么是灰度发布 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用. 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为 ...

  7. [USACO09MAR]向右看齐Look Up(单调栈、在线处理)

    https://www.luogu.org/problem/P2947 题目描述 Farmer John's N (1 <= N <= 100,000) cows, convenientl ...

  8. cat命令的一个用法

    1:  cat 1.txt 2.txt 3.txt > 4.txt  这个操作可以把前三个文件的内容全部复制到第四个文件中去

  9. elasticsearch-hadoop 扩展定制 官方包以支持 update upsert doc

    官方源码地址https://github.com/elastic/elasticsearch-hadoop 相关文档 https://www.elastic.co/guide/en/elasticse ...

  10. 尝试brpc来升级rpc服务,测试应用过程

    照着官方文档来,不过在mac下还是有些小坑 对熟悉c++的人来说很小儿科,但对c++相对比较外行 (只知道基本语法和部分数据结构)的人,还是作不到开箱即用 首先编译```If you need to ...