1. You are given an n x n 2D matrix representing an image.
  2.  
  3. Rotate the image by 90 degrees (clockwise).
  4.  
  5. Follow up:
  6. Could you do this in-place?

The rotation can be performed in layers, where you perform a cyclic swap on the edges on

each layer In the frst for loop, we rotate the frst layer (outermost edges) We rotate the
edges by doing a four-way swap frst on the corners, then on the element clockwise from the
edges, then on the element three steps away
Once the exterior elements are rotated, we then rotate the interior region’s edge

  1. class Solution {
  2. public:
  3. void rotate(vector<vector<int> > &matrix) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. int n = matrix.size();
  7. if(n < ) return ;
  8. for(int lay = ; lay < n/; lay ++){
  9. int first = lay ;
  10. int last = n - - lay;
  11. for(int i = first; i< last; i++){
  12. int offset = i - first;
  13. //store the top
  14. int top = matrix[first][i] ;
  15. //left to top
  16. matrix[first][i] = matrix[last - offset][first];
  17. //bottom to left
  18. matrix[last - offset][first] = matrix[last][last- offset];
  19. //right to bottom
  20. matrix[last][last - offset] = matrix[i][last];
  21. //top to right
  22. matrix[i][last] = top;
  23. }
  24. }
  25. }
  26. };

LeetCode_Rotate Image的更多相关文章

  1. LeetCode_Rotate List

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

随机推荐

  1. java_内存划分

    内存划分

  2. java.lang.NoSuchFieldError: deferredExpression解决

       java.lang.NoSuchFieldError: deferredExpression这个问题的出现是在的lib下面有多个版本的jstl.jar包,解决办法很简单,只留下一个版本的jstl ...

  3. zoj 3547 The Boss on Mars

    需要用到概率论的容斥定理以及计算1 ^ 4 + 2 ^ 4 + ……+ n ^ 4的计算公式1^4+2^4+……+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30 #pragma comm ...

  4. 软件project(五)——可行性研究

    一.目的 用最小的代价高效率的确定问题是否可以解决. 不是去解决这个问题,而是确定问题是否值得去解决.进行可行性研究简化了系统分析和系统设计的过程. 二.任务 (1)进一步分析问题定义. (2)分析员 ...

  5. Ubuntu下访问SSH

    ssh程序分为有客户端程序openssh-client和服务端程序openssh-server.如果需要ssh登陆到别的电脑,需要安装openssh-client,该程序ubuntu是默认安装的.而如 ...

  6. c++ 11 多线程教学(1)

    本篇教学代码可在GitHub获得:https://github.com/sol-prog/threads. 在之前的教学中,我展示了一些最新进的C++11语言内容: 1. 正则表达式(http://s ...

  7. AIX 常用命令和知识

      BOOTLIST:#bootlist -m normal -o (查看bootlist)#bootlist -m normal (设置bootlist为空,谁要在我机器上执行我就要哭了)#boot ...

  8. Android Studio IDE 所遇问题汇总

    [窗体视图无法显示]     在/res/values/styles.xml文件中,修改一下内容 <!-- Base application theme. --><style nam ...

  9. 《第一行代码》学习笔记12-UI(1)

    1.程序需要注销或者退出,用一个专门的集合类对所有的活动进行管理即可. 2.可视化编辑工具不利于真正了解界面背后的实现原理,通常这种方式制作的界面都不具有很好的屏幕适配性, 而且当需要编写较为复杂的界 ...

  10. WCF Service端Inspector

    问题 在使用WCF的过程中,有时候需要在service端截取client和service之间的消息来做一些如写log,检查message是否合法的操作. 那么如何才能实现呢? 解决方案 使用WCF提供 ...