给定一个 n × n 的二维矩阵表示一个图像。

将图像顺时针旋转 90 度。

说明:

你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

示例 1:

给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ]

示例 2:

给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [15,14,12,16] ], 原地旋转输入矩阵,使其变为: [ [15,13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7,10,11] ]

顺时针旋转矩阵的步骤

1.把矩阵上下翻转。比如[1,2][3,4] -> [3, 4][1, 2]

2.将矩阵沿着从左上角到右下角的对角线进行翻转

如果是逆时针

则将1,2步倒着操作

如果记不住顺序,可以试着用二维数组模拟一下

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

Leetcode48. Rotate Image旋转图像的更多相关文章

  1. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

  2. [LeetCode] Rotate Image 旋转图像

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

  3. LeetCode48 Rotate Image

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

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

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

  5. [leetcode]48. Rotate Image旋转图像

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

  6. 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 旋转图像

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

  8. [CareerCup] 1.6 Rotate Image 翻转图像

    1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...

  9. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

随机推荐

  1. SpringBoot学习笔记(八):SpringBoot启动端口+访问路、SpringBoot配置文件yml、SpringBoot多环境区分、SpringBoot打包发布

    SpringBoot启动端口+访问路径 配置文件: server.port=9090 server.context-path=/springboot 现在只能用http://127.0.0.1:909 ...

  2. Linux查看温度

    step 1: centos $ sudo yum install lm_sensors ubuntu $ sudo apt-get install lm_sensors step2$ sudo se ...

  3. java面向对象特征 — 一句话概括

    java基础学习总结之基本特征,最开始学习的时候,是形而上的理解,用了3年多,再回头看,理解起来颇为顺理成章 语言学习大概就是这样一种规律,学习,不甚解,应用,应用,渐深入人心,回头一看,恍然一悟 最 ...

  4. NYOJ--860 又见01背包(01背包)

    题目http://acm.nyist.net/JudgeOnline/problem.php?pid=860 分析:题目和普通的01背包问题一样,但是唯一不同的是数据的特殊性. 如果10^9根本就开辟 ...

  5. 如何使用Pig集成分词器来统计新闻词频?

    散仙在上篇文章中,介绍过如何使用Pig来进行词频统计,整个流程呢,也是非常简单,只有短短5行代码搞定,这是由于Pig的内置函数TOKENIZE这个UDF封装了单词分割的核心流程,当然,我们的需求是各种 ...

  6. 2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...

    title author date CreateTime categories WPF shows that some windows in multithreading will be locked ...

  7. Spring Security Web应用入门环境搭建

    在使用Spring Security配置Web应用之前,首先要准备一个基于Maven的Spring框架创建的Web应用(Spring MVC不是必须的),本文的内容都是基于这个前提下的. pom.xm ...

  8. Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...

  9. vs2013+opencv2410的一些问题

    1.设置microsoft.user时,debug和relaese只能修改一次,相对应的32和64会相应修改,因此可以新建一个属性表,命名为OpenCV2410debug: 2.报错:error LN ...

  10. JasperReports报表组15

    组在JasperReports的协助组织对报告的数据以逻辑方式.报告组代表连续记录的数据源中有一些共同点,比如某个报表字段的值的序列.报告组由<group>元素定义.一个报表可以有任意数量 ...