leetcode 048 Rotate Image
2016-04-29 22:14
344 查看
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?
Subscribe to see which companies asked this question
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
int uprow = 0, downrow = rows-1, leftcol = 0, rightcol = cols-1;
for(; uprow < downrow; uprow++, downrow--) {
int ax = uprow, ay = leftcol;
int bx = uprow, by = rightcol;
int cx = downrow, cy = leftcol;
int dx = downrow, dy = rightcol;
int cnt = downrow-uprow;
while(cnt--) {
int temp = matrix[bx][by];
matrix[bx][by] = matrix[ax][ay];
matrix[ax][ay] = matrix[cx][cy];
matrix[cx][cy] = matrix[dx][dy];
matrix[dx][dy] = temp;
ay+=1;
bx+= 1;
cx-=1;
dy-=1;
}
leftcol++;
rightcol--;
}
}
};
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
Subscribe to see which companies asked this question
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = matrix[0].size();
int uprow = 0, downrow = rows-1, leftcol = 0, rightcol = cols-1;
for(; uprow < downrow; uprow++, downrow--) {
int ax = uprow, ay = leftcol;
int bx = uprow, by = rightcol;
int cx = downrow, cy = leftcol;
int dx = downrow, dy = rightcol;
int cnt = downrow-uprow;
while(cnt--) {
int temp = matrix[bx][by];
matrix[bx][by] = matrix[ax][ay];
matrix[ax][ay] = matrix[cx][cy];
matrix[cx][cy] = matrix[dx][dy];
matrix[dx][dy] = temp;
ay+=1;
bx+= 1;
cx-=1;
dy-=1;
}
leftcol++;
rightcol--;
}
}
};
相关文章推荐
- 8.oracle的dump理解八 dump文件头
- Cglib生成动态代理(有改动)
- 【算法】生僻算法复习
- Linux内核学习总结
- windows环境下把Python代码打包成独立执行的exe可执行文件
- 一如研途深似海,从此轻松是路人
- CodeForces - 581A Vasya the Hipster (水)
- 在RaspberryPi中用CasperJS对网页元素截图
- Ubuntu下查看linux版本,内核版本,系统位数,gcc版本
- MVC中利用ActionFilterAttribute过滤关键字
- 7.oracle的dump理解七 enqueue理论及dump enqueues
- GO语言下载、安装、配置
- Codeforces Round #347 (Div. 2)-Complicated GCD(字符串判等)
- 团队冲刺第八天
- Openstack平台neutron模块之网络基础概念
- 5-1前的焦虑
- HDU 2191 多重背包
- lightoj1045 - Digits of Factorial
- 4.latch之oracle latch undo globaldata
- 如何模拟cursor pin S