LeetCode_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?
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的更多相关文章
- LeetCode_Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
随机推荐
- KEIL 伪指令
//为了大家查找方便,命令按字母排序:0.ALTNAME 功能: 这一伪指令用来自定义名字,以替换源程序中原来的保留字,替换的保留字均可等效地用于子程序中. 格式: ALTNAME 保留字 自定义名 ...
- Git for windows GUI使用
试用了下CSDN的Code 期间还遇到一个错误 http://blog.csdn.net/utstarm/article/details/8249853 这个新手教程写得不错 https://code ...
- logstash 发送zabbix告警
<pre name="code" class="html">[elk@dr-mysql01 test]$ cat t1.conf input { s ...
- python部落刷题宝学到的内置函数
最近加入了python部落,感觉里面的刷题宝很有意思,玩了一下,知道了许多以前并不清楚的内置函数,然后感觉到快要记不住了,所以开始陈列一下 1.divmod(a,b):取a除以b的商和余数,功效等价于 ...
- editplus批量删除html代码空行
在editplus替换菜单功能里,“查找”功能里输入: ^[ \t]*\n 替换为空,然后“全部替换”即可. 替换时,要选择“正则表达式”选项, 详细:http://www.dedecms8.com/ ...
- ajax提交写法
<script> /* ajax提交写法 */ function add_prize() { // var query={}; var query = new Object(); quer ...
- css如何使背景图片水平居中
CSS中定位背景图片的属性是:background-position,用法background-position 属性设置背景图像的起始位置. 你要水平居中可以: div{background-pos ...
- hdu2531之BFS
Catch him Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Wikioi 1080一维树状数组
半个月时间最终把那些杂七杂八的学完了,尽管学完也,也仅仅是有了个模板,自己手敲还是不太行.所以如今開始要疯狂刷题了! ! .!!! 这题裸的树状数组.曾经写那道<敌兵布阵>的时候写过,所以 ...
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)
Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...