题目

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. Phpstorm安装和优化

    Phpstorm是php开发一个强大的IDE,但是它不是免费的需要注册码,而且界面是英文界面,对英文不太好的人有点不友好.所以这篇文章主要从phpstorm的破解和汉化来优化phpstorm. 1.首 ...

  2. python的安装教学

    1.首先登陆到python的官方网站 https://www.python.org/ 2.鼠标放在Download上,点击下面对应的型号,我的是Windows 3.点击Windows到此页面,点击3. ...

  3. 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...

  4. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

  5. Create the first sql server 2016 mobile report;创建 第一个 sqlserver 2016 Mobile report

    在微软收购了datazen之后,sqlserver2016 集成了mobilereport,mobile report 基于html5,兼容各类主流浏览器,之前ssrs2008 R2中很多chart类 ...

  6. Mybatis事务处理

    知识点有事务处理的配置,还有事务处理的方法 事务处理的配置: mybatis的事务处理由两种方式控制,JDBC和MANAGED:  MANAGED就是说事务处理由第三方的插件来完成,比如说spring ...

  7. 171 Excel Sheet Column Number Excel表列序号 26进制转10进制

    给定一个Excel表格中的列名称,返回其相应的列序号.示例:    A -> 1    B -> 2    C -> 3    ...    Z -> 26    AA -&g ...

  8. No rule to make target ...

    在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...

  9. 安卓(Android )软键盘的控制(显示和隐藏)

    Activity 启动时软键盘默认状态 在清单文件(manifest .xml)中可以通过在 Activity 标签中增加属性控制软键盘的默认状态: android:windowSoftInputMo ...

  10. Apache Tomcat 之路(三 部署多个应用)

    想要在一台服务器上部署多个web应用的时候有两种部署方式:1.拷贝多个tomcat 服务器,每个服务器启动不同的web应用;2.一个tomcat容器部署多个web应用 两种方式的优缺点:多个tomca ...