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. php empty函数

    empty — 检查一个变量是否为空. 当一个变量并不存在,或者它的值等同于FALSE,那么它就会被认为不存在.如果变量不存在的话,empty()并不会产生警告. 返回值: 当var存在,并且是一个非 ...

  2. Orchard源码分析(7):ASP.NET MVC相关

    概述 Orchard归根结底是一个ASP.NET MVC(以后都简称为MVC)应用,但在前面的分析中,与MVC相关内容的涉及得很少.MVC提供了非常多的扩展点,本文主要关注Orchard所做的扩展.主 ...

  3. jQuery版本升级踩坑大全

    背景 -------------------------------------------------------------------------------- jQuery想必各个web工程师 ...

  4. 【转】 C++使用zlib库(-)

    来自:  http://blog.chinaunix.net/uid-24607609-id-2118143.html   今天看到一个gzopen函数,搜了一下他的系列函数,及相关用法   C++使 ...

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

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

  6. 看开源代码利器—用Graphviz + CodeViz生成C/C++函数调用图(call graph)

    一.Graphviz + CodeViz简单介绍 CodeViz是<Understanding The Linux Virtual Memory Manager>的作者 Mel Gorma ...

  7. VTK初学一,a_Vertex图形点的绘制

    系统:Win8.1 QT版本:2.4.2,Mingw VTK版本:6.3 2. main.cpp #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #incl ...

  8. html5开发制作,漂亮html5模板欣赏,H5网站建设

    html5是什么? HTML5 是下一代的 HTML(超文本标记语言,网页的组成部分),HTML5是web开发世界的一次重大的改变,能适配pc.手机等各终端,跨平台性能极强,移动互联网是未来的趋势,h ...

  9. 63.Hbase 常用命令

    1.进入 Hbase shell ./hbase shell 2. 命令 1.list 2.truncate 3.scan 4.describe 5.create 'tablename','famil ...

  10. ICloud没有密码怎么注销?

    忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...