作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/rotate-image/description/

题目描述

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

题目大意

把矩阵原地顺时针旋转90度。

解题方法

做法挺简单,先上下翻转,再延左上到右下的对角线进行翻转(镜像操作)。

需要注意的是上下翻转的时候是rows-i-1,而不是rows-i。

代码:

class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
if matrix:
rows = len(matrix)
cols = len(matrix[0])
for i in xrange(rows / 2):
for j in xrange(cols):
matrix[i][j], matrix[rows - i - 1][j] = matrix[rows - i - 1][j], matrix[i][j]
for i in xrange(rows):
for j in xrange(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

二刷的时候使用的另外一种旋转的方法:先左右镜像翻转,然后再沿着副对角线(从右上到左下的对角线)进行翻转。比上面的麻烦一点。

使用的C++代码如下:

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
if (!matrix.size()) return;
const int N = matrix.size();
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N / 2; j ++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][N - j - 1];
matrix[i][N - j - 1] = tmp;
}
}
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N - i; j ++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[N - j - 1][N - i - 1];
matrix[N - j - 1][N - i - 1] = tmp;
}
}
}
};

日期

2018 年 3 月 5 日
2018 年 12 月 30 日 —— 周赛差强人意

【LeetCode】48. Rotate Image 解题报告(Python & C++)的更多相关文章

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

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

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

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

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

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 在windows下使用shell,运行shell脚本

    在Windows操作系统下运行Shell脚本,缺少的只是一个Git软件.其下载路径为Git - Downloading Package. 安装之后,将安装路径下的bin文件夹的路径作为环境变量.于是我 ...

  2. SimpleNVR如何把安防监控画面推流到微信公众号直播

    背景需求 进入移动互联网时代以来,微信已成为许多企业除官网以外必备的宣传渠道,当3.2亿直播用户与九亿微信用户的势能增加,在微信上开启直播已成为越来越多企业的不二选择. 需求分析 微信公众号作为平台来 ...

  3. HDFS06 DataNode

    DataNode 目录 DataNode DataNode工作机制 数据完整性 DataNode掉线时限参数设置 DataNode工作机制 一个数据块在DataNode上以文字形式存储在磁盘上,包括一 ...

  4. 日常Java 2021/10/17

    今天开始Javaweb编译环境调试,从tomcat容器开始,然后mysql的下载,连接工具datagrip,navicat for mysql,然后就是编写自己的sql,安装jdbc,eclipse连 ...

  5. ceph对象存储场景

    安装ceph-radosgw [root@ceph-node1 ~]# cd /etc/ceph # 这里要注意ceph的源,要和之前安装的ceph集群同一个版本 [root@ceph-node1 c ...

  6. Shell脚本的条件控制和循环语句

    条件判断:if语句 语法格式: if [ expression ] then Statement(s) to be executed if expression is true fi 注意:expre ...

  7. OAuth2.0实战:认证、资源服务异常自定义!

    大家好,我是不才陈某~ 这是<Spring Security 进阶>的第4篇文章,往期文章如下: 实战!Spring Boot Security+JWT前后端分离架构登录认证! 妹子始终没 ...

  8. centos7部署二进制mysql-5.6

    目录 一.环境声明 二.程序部署 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql-5.6.39 3 ...

  9. shell脚本 系统信息检测

    一.简介 源码地址 日期:2018/4/12 介绍:根据指令展示不同的系统数据 效果图: 二.使用 适用:centos6+ 语言:中文 注意:无 下载 wget https://raw.githubu ...

  10. java多线程7:ReentrantReadWriteLock

    真实的多线程业务开发中,最常用到的逻辑就是数据的读写,ReentrantLock虽然具有完全互斥排他的效果(即同一时间只有一个线程正在执行lock后面的任务), 这样做虽然保证了实例变量的线程安全性, ...