题目

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

分析

本地使得二维矩阵,旋转90角度。

通过实际数据分析,通过两个步骤的元素交换可实现目标:

  1. 按照主对角线,将对称元素交换
  2. 按照列,将对称列元素全部交换

即可达到,使得二维矩阵,本地旋转90个角度。

AC代码

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
if (matrix.empty())
return; int n = matrix.size(); //首先,沿主对角线交换元素
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
swap(matrix[i][j], matrix[j][i]);
} for (int i = 0, j = n - 1; i < j; i++, j--)
{
for (int k = 0; k < n; k++)
swap(matrix[k][i], matrix[k][j]);
}
}
};

GitHub测试程序源码

LeetCode(48)Rotate Image的更多相关文章

  1. LeetCode(61) Rotate List

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

  2. 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 arr ...

  3. LeetCode(48):旋转图像

    Medium! 题目描述: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  5. Thinkphp入门 四 —布局、缓存、系统变量 (48)

    原文:Thinkphp入门 四 -布局.缓存.系统变量 (48) [控制器操作方法参数设置] http://网址/index.php/控制器/操作方法 [页面跳转] [变量调节器] Smarty变量调 ...

  6. Windows Phone开发(48):不可或缺的本地数据库

    原文:Windows Phone开发(48):不可或缺的本地数据库 也许WP7的时候,是想着让云服务露两手,故似乎并不支持本地数据库,所有数据都上传上"云"数据库中.不过呢,在SD ...

  7. Qt 学习之路 2(48):QSortFilterProxyModel

    Qt 学习之路 2(48):QSortFilterProxyModel 豆子 2013年4月11日 Qt 学习之路 2 6条评论 从本章开始,我们将逐步了解有关自定义模型的相关内容.尽管前面我们曾经介 ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. 原生js一行代码实现简易轮播图

    这是一个简易的js无限循环轮播图,只用了一行js代码就实现了无限循环,记录一下三目运算符的伟大! <!DOCTYPE html><html lang="en"&g ...

  2. 第一篇(那些JAVA程序BUG中的常见单词)

    The local variable xxx may not have been initialized. 局部变量xxx可能尚未初始化 local variable 局部变量 initialized ...

  3. How many Fibs? POJ - 2413

    How many Fibs? POJ - 2413 高精模板 #include<cstdio> #include<cstring> #include<algorithm& ...

  4. Spring配置文件中未引入dubbo命名空间头部配置而引起的错误案例

    问题描述: Spring配置文件中未引入dubbo命名空间的头部配置而引起项目启动时报出如下错误信息: org.springframework.beans.factory.xml.XmlBeanDef ...

  5. Snort里如何将读取的包记录存到指定的目录下(图文详解)

    不多说,直接上干货! 比如,在/root/log目录下. [root@datatest ~]# snort -dve -l /root/log 需要注意: 1) /log目录需要你自己建立,并修改权限 ...

  6. 【深入.NET平台】浅谈.NET Framework基元类型

    什么是基元类型? 初学者可能很少听说过这个名词,但是平时用得最多的肯定是基元类型.先看下面两行代码: System.Int32 a = ; ;  上面两行代码都表示声明一个int类型的变量,但在平时写 ...

  7. subprocess模块和sys模块

    1.import sys # sys.path # sys.argv # 用来接收python解释器执行py文件后跟的参数#例如:python cp.py argv1 argv2 arg3#sys.a ...

  8. Swiper插件轮播

    <html><head> <meta charset="utf-8"> <title>Swiper轮播</title>& ...

  9. mysql 修改 root 密码

    5.76中加了一些passwd的策略 MySQL's validate_password plugin is installed by default. This will require that ...

  10. ESSENTIALS OF PROGRAMMING LANGUAGES (THIRD EDITION) :编程语言的本质 —— (一)

    # Foreword> # 序 This book brings you face-to-face with the most fundamental idea in computer prog ...