题目

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. NOI题库--盒子和小球系列 By cellur925

    题目传送门 盒子和小球之二:N个有差别的盒子(1<=N<=20).你有A个红球和B个蓝球.0 <= A <= 15, 0 <= B <= 15.球除了颜色没有任何区 ...

  2. 原生JavaScript实战之搜索框筛选功能

    成品图如下所示: 先搭建HTML结构: <div class="wrapper"> <div class="sWrapper"> < ...

  3. ROS学习笔记十二:使用gazebo在ROS中仿真

    想要在ROS系统中对我们的机器人进行仿真,需要使用gazebo. gazebo是一种适用于复杂室内多机器人和室外环境的仿真环境.它能够在三维环境中对多个机器人.传感器及物体进行仿真,产生实际传感器反馈 ...

  4. _bzoj1051 [HAOI2006]受欢迎的牛【强联通】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051 保存tarjan模版. 求强联通分量,缩点. #include <cstdio& ...

  5. POJ3320 Jessica's Reading Problem

    Bryce1010模板 #include <stdio.h> #include <string.h> #include <stdlib.h> #include &l ...

  6. BitCoin工作原理

    1.加密货币 公共账本-信任+加密算法=加密货币 BitCoin是第一个被是实现出来的加密货币. 首先理解比特币是什么,在考虑要不要买入?(人人都想一夜暴富,美哉) 2.发送.接收.创造比特币的时候电 ...

  7. 51nod 1126 求递推序列的第N项

    1126 求递推序列的第N项  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 有一个序列是这样定义的:f(1) = 1, f(2) = 1, f( ...

  8. 10.3 Implementing pointers and objects and 10.4 Representing rooted trees

    Algorithms 10.3 Implementing pointers and  objects  and 10.4 Representing rooted trees Allocating an ...

  9. CAS4.0 server 环境的搭建

    1.上cas的官网下载cas server 官网地址:https://github.com/Jasig/cas/releases,下载好后 解压下载的 cas-server-4.0.0-release ...

  10. <在此处打开命令窗口>替换为PowerShell打开模式

    Windows7中Shift+右键"在此处打开命令窗口"默认是采用cmd的方式打开. 把cmd替换为PowerShell的方式打开. 1. Ctrl + R 输入regedit进入 ...