Rotate Image

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?

 
 
假设旋转的时候,左上角元素为(l,l),右下角元素为(r,r)
则旋转的时候
(l,l+1)=(r-1,l) 最上面一排 
(r-1,l)=(r,r-1)左边一排
(r,r-1)=(l+1,r)下面一排
(l+1,r)=(l,l+1)右边一排
 
可以找到规律
(i,j)--->(r+l-j,i)
 

 class Solution {
public:
void rotate(vector<vector<int> > &matrix) { int n=matrix.size();
int leftUp=;
int rightBottom=n-;
while(rightBottom>leftUp)
{
int n0=rightBottom+leftUp;
for(int i=leftUp;i<rightBottom;i++)
{
int tmp=matrix[leftUp][i];
matrix[leftUp][i]=matrix[n0-i][leftUp];
matrix[n0-i][leftUp]=matrix[n0-leftUp][n0-i];
matrix[n0-leftUp][n0-i]=matrix[i][n0-leftUp];
matrix[i][n0-leftUp]=tmp; } leftUp++;rightBottom--;
}
}
};
 
 

【leetcode】Rotate Image的更多相关文章

  1. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  2. 【leetcode】Rotate List(middle)

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

  3. 【leetcode】Rotate Image(middle)

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

  4. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  5. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

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

  6. 【LeetCode】Rotate List

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

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

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

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

随机推荐

  1. GoLang之方法与接口

    GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...

  2. MYSQL双主故障解决实例。

    根据报错得知,获取到的主库文件格式错误. 一.锁住主从正常的库 Mysql> flush tables with read lock; 锁表 unlock tables; 解锁 SHOW MAS ...

  3. weblogic 的安装和配置

    一.安装 1.1安装weblogic8.1 首先从www.bea.com上下载安装文件platform816_linux32.bin,然后在安装文件所在目录下键入 ./platform816_linu ...

  4. Android Studio-设置鼠标悬停显示方法声明

  5. 【8-19】java学习笔记01

    JDK API文档 java SE 8 API文档:http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downl ...

  6. 为什么构造器不能是abstract, static, final, native or synchronized的?

    Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized. 1. A const ...

  7. jquery eval解析JSON中的注意点介绍

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式:使用eval()函数.使用Function对象来进行返回解析,下面有个示例,感兴趣的朋友可以参考下   在JS中将JSON的字符串解析 ...

  8. 2015年12月01日 GitHub入门学习(三)GitHub创建仓库

    序:创建自己的GITHub账号,并创建自己第一个仓库,尝试通过msysgit客户端,往仓库提交文件. 一.创建GitHub账户 链接地址:https://github.com/join,很简单,自己创 ...

  9. 在ashx中使用Server对象

    Server.MapPath() System.Web.HttpContext.Current.Server.MapPath()

  10. 3_mysql 主从复制

    mysql 主从复制 网易数据库 石勇 提纲 什么是主从复制 主从复制的原理 主从复制的用途 主从复制的搭建 主从复制的问题 什么是主从复制 数据拷贝 准实时 源-主节点:目的-从节点 主从复制的原理 ...