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 ...
随机推荐
- JavaScript中的Array对象
1.创建Array对象创建Array对象的语法var 数组名 = new Array();定义数组之后,就需要向数组中添加元素,格式如下数组名[<下标>]=值: 2.Array对象属性Ar ...
- Unity中的关节
关节组件一共分为5大类,它们分别是链条关节.固定关节.弹簧关节.角色关节和可配置关节.链条关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产 ...
- Keil 3光标问题 以及汉字问题
初次使用keil3,光标总是定位不准,修改十分麻烦,google后解决问题,修改tools.ini如下(蓝色为加入项): NAME="YGLenovo User", "a ...
- QEventLoop 的使用两例
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- mysql创建utf-8字符集数据库
CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; CREATE DATABASE 的语法:CREA ...
- 在eclipse中创建web项目
如何创建dynamic web project项目 本文的演示是从本地文件创建dynamic web project,从svn检出的同时创建dynamic web project于此类似.我们推荐使用 ...
- <php>json小结
1.页面中如果即用到jquery包,又用到js文件,要先写jquery包,再加载js文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- hdu 4586 Play the Dice(概率dp)
Problem Description There is a dice with n sides, which are numbered from 1,2,...,n and have the equ ...
- poj 3176 Cow Bowling(dp基础)
Description The cows don't use actual bowling balls when they go bowling. They each take a number (i ...
- Web Service-- 使用 JDK 发布 WS
Web Service,即“Web 服务”,简写为 WS,从字面上理解,它其实就是“基于 Web 的服务”.而服务却是双方的,有服务需求方,就有服务提供方.服务提供方对外发布服务,服务需求方调用服务提 ...