本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Rotate Image

Total Accepted: 15609 Total
Submissions: 49679My Submissions

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?

题意:给定一个 n * n 的二维图像。将该图像顺时针旋转 90 度

思路:

先沿副对角线翻转一次,再沿水平中线翻转一次

复杂度:时间O(n^2),空间O(1)

void rotate(vector<vector<int> > &matrix){
int n = matrix.size();
//沿副对角线翻转
for(int i = 0; i < n; ++i){
for(int j = 0; j < n - i; ++j){
int i2 = n - 1 - j, j2 = n - 1 - i;
swap(matrix[i][j], matrix[i2][j2]);
}
}
//沿水平中线翻转
for(int i = 0; i < n/2; ++i){
swap(matrix[i], matrix[n - i - 1]);
}
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Leetcode 实施细节 Rotate Image的更多相关文章

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

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

  2. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  3. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...

  4. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  5. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  6. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  7. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  8. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  9. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

随机推荐

  1. spring配置日志

    原文:http://blog.csdn.net/xiejx618/article/details/41698913 参考:http://spring.io/blog/2009/12/04/loggin ...

  2. FMOD在Android玩音响系统的抖动问题

    1. 基本介绍 在Android升级系统Android4.4之后,发现FMOD在Android音会出现抖动.导致声音不正常.边赫赫有名的"极品飞车"都有问题. 经查验,是FMOD的 ...

  3. SVN的revert和update命令的区别

    svn中的revert和update 今天有人问到revert和update的问题. 刚开始还真被问住了. 因为感觉revert和update都可以将本地的copy更新到以前的一个版本,会有什么不同呢 ...

  4. java日历程序版本

    //MainFrame.java package com.huowolf.myCalender; import java.awt.BorderLayout; import java.awt.Color ...

  5. registerWithTouchDispatcher()函数的使用

    registerWithTouchDispatcher()函数的使用 registerWithTouchDispatcher()函数主要用于注册Touch事件. 当我们使用this->setTo ...

  6. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  7. windows phone (17) ManipulationDelta事件

    原文:windows phone (17) ManipulationDelta事件 ManipulationDelta事件会是在触摸位置发生变化是引发,比如可以根据用户在触摸屏中移动的位置,图片发生相 ...

  8. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)

    4 三维 4.1 共享三维场景         用户能够将ArcScene文档导出为3D web场景,能够被加载到ArcGIS Online.Portal或本地Web服务器上并进行分享.这样,用户可以 ...

  9. 使用Xamarin在Visual Studio中开发Android应用

    原文:使用Xamarin在Visual Studio中开发Android应用 本文使用的环境是Windows 8 Visual Studio 2012.2 1.下载Xamarin http://xam ...

  10. 解决alaert.builder二次调用报错的bug

    报错的代码是: The specified child already has a parent. You must call removeView() on the child's parent f ...