题目:

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]
]

题解:

  暴力解

Solution 1

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n / ; ++i){
for(int j = i; j < n - - i; ++j){
int tmp = matrix[i][j];
matrix[i][j] = matrix[n - - j][i];
matrix[n - - j][i] = matrix[n - - i][n - - j];
matrix[n - - i][n - - j] = matrix[j][n - - i];
matrix[j][n - - i] = tmp;
}
}
}
};

Solution 2

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n; ++i){
for(int j = ; j < n - i; ++j){
swap(matrix[i][j], matrix[n - - j][n - - i]);
}
}
for(int i = ; i < n / ; ++i){
for(int j = ; j < n; ++j){
swap(matrix[i][j], matrix[n - - i][j]);
}
}
}
};

先沿着副对角线(/)翻转,再沿着水平中线翻转。

Solution 3

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n / ; ++i){
for(int j = ; j < n; ++j){
swap(matrix[i][j], matrix[n - - i][j]);
}
}
for(int i = ; i < n; ++i){
for(int j = ; j < i; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

先沿着水平中线翻转,再沿着主对角线(\)翻转。

Solution 4

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = ; i < n; ++i){
for(int j = ; j < i; ++j){
swap(matrix[i][j], matrix[j][i]);
}
}
for(int i = ; i < n; ++i){
for(int j = ; j < n / ; ++j){
swap(matrix[i][j], matrix[i][n - - j]);
}
}
}
};

先对原数组取其转置(即沿着主对角线翻转),然后把每行的数字翻转(沿着竖直中线翻转)。另,Solution 4 也可写为Solution 4.1

Solution 4.1

class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
for (int i = ; i < n; ++i) {
for (int j = i + ; j < n; ++j) {
swap(matrix[i][j], matrix[j][i]);
}
reverse(matrix[i].begin(), matrix[i].end());
}
}
};

【LeetCode】048. Rotate Image的更多相关文章

  1. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  2. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  3. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  4. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】189 - Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. 【leetcode】61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【LeetCode】48. Rotate Image (2 solutions)

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

  8. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...

  9. 【LeetCode】796. Rotate String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. swift中的?和!理解

    本文转载至 http://www.cnblogs.com/dugulong/p/3770367.html 首先贴cocoachina上某位大大的帖子:     Swift语言使用var定义变量,但和别 ...

  2. kubernetes高级之集群中使用sysctls

    系列目录 在linux系统里,sysctls 接口允许管理员在运行时修改内核参数.参数存在于/proc/sys/虚拟进程文件系统里.参数涉及到很多子模块,例如: 内核(kernel)(常见前缀kern ...

  3. Spring 和 filter

    标题是 spring和filter,但是这里却是说的spring MVC 项目中需要用到filter,filter中需要用到spring实例化的bean,于是为了简化就形成spring和filter了 ...

  4. WCF基础之消息协定

    通常定义消息的架构,使用数据协定就够了,但是有时必须将类型精确映射到soap消息,方法两种:1.插入自定义soap标头:2.另一种是定义消息的头和正文的安全属性.消息协定通过MessageContra ...

  5. 从内存中加载并启动一个exe

    windows似乎只提供了一种启动进程的方法:即必须从一个可执行文件中加载并启动.而下面这段代码就是提供一种可以直接从内存中启动一个exe的变通办法.用途嘛, 也许可以用来保护你的exe,你可以对要保 ...

  6. 使用JSTL在页面前的空行怎么去除?

    解决的方法是:在每个JSP的头上加上一段代码   <%@ page trimDirectiveWhitespaces="true" %>

  7. 找出旋转有序数列的中间值python实现

    题目给出一个有序数列随机旋转之后的数列,如原有序数列为:[0,1,2,4,5,6,7] ,旋转之后为[4,5,6,7,0,1,2].假定数列中无重复元素,且数列长度为奇数.求出旋转数列的中间值.如数列 ...

  8. VHDL基础 学习笔记

    最近一直忙着学校里的活动,所以没怎么更新,上周活动忙完了,正好也借着数电实验的机会,重新学习一下VHDL的编程.以下是转自360doc的教程: ———————————————————————————— ...

  9. [原创]java WEB学习笔记39:EL中的运算符号(算术运算符,关系运算符,逻辑运算符,empty运算符,条件运算符,括号运算符)

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  10. 使用deepfashion实现自己的第一个分类网络

    这个过程主要分为三个步骤: 数据预处理 数据处理就是把数据按照一定的格式写出来,以便网路自己去读取数据 1准备原始数据 我的cloth数据一共是四个类别,每个类别有衣服47张,一用是188张图片,这些 ...