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?

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

class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = matrix.size();
if(n < ) return ;
for(int lay = ; lay < n/; lay ++){
int first = lay ;
int last = n - - lay;
for(int i = first; i< last; i++){
int offset = i - first;
//store the top
int top = matrix[first][i] ;
//left to top
matrix[first][i] = matrix[last - offset][first];
//bottom to left
matrix[last - offset][first] = matrix[last][last- offset];
//right to bottom
matrix[last][last - offset] = matrix[i][last];
//top to right
matrix[i][last] = top;
}
}
}
};

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. 开发支付宝支付用DELPHI实现 RSA签名

    近来根据业务需求 在ERP中集成了微信支付,支付宝支付,开发支付宝支付时最大的障碍就是RSA签名,找了很多资料,最终用 下了个libeay32.pas  根据网上资料最终解决了问题 function  ...

  2. Linux下cut命令用法

    1 一两句话描述一下cut命令吧! 正如其名,cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的. cut是以每一行为一个处理对象的,这种机制和sed是一样的.(关于sed的入门文章将在近期 ...

  3. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

  4. 【转】将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)

    原文网址:http://blog.csdn.net/bokee/article/details/6633193 工欲善其事,必先利其器.一个强大的开发环境可以大大提高工作效率.好吧,我知道这是废话.. ...

  5. jquery 实现全选反选

    jquery代码 $(function () { $('#inputCheck').click(function () { if ($(this).attr("checked")) ...

  6. cf493B Vasya and Wrestling

    B. Vasya and Wrestling time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. cf445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. WP系统推广难的原因之中的一个之我见

    个人也觉得如今的Android市场几家独大,竞争太激烈.利润空间挤压太严重,有实力的厂家不如尝试剑走偏锋,在其它大佬都还没跟进的时候,把市场投向WP.先入为主,不失为良策! 话说Microsoft不开 ...

  9. 关于时间的操作(JavaScript版)——年月日三级级联(默认依次显示请选择年、请选择月和请选择日)

    这篇博客和前一篇博客基本同样,仅仅是显示的默认值不同: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN&quo ...

  10. linux 终止用户会话

    第一步使用 tty 命令 查看自己会话id:本例中会话id为1[root@localhost ~]# tty/dev/pts/1[root@localhost ~]# 第二步 使用 w 命令 查看当前 ...