- 首先我要在纸上,非常非常聪明且迅速且机灵,
- 给出几个用例,找出边界用例和特殊用例,确定特判条件;在编码前考虑到所有的条件
- 向面试官提问:问题规模,特殊用例
- 给出函数头
- 暴力解,简述,优化。
- 给出能够想到的最优价
- 伪代码,同时结合用例
- 真实代码
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?
/*
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?
*/
void rot(int x, int y, vector<vector<int> > &mtx)
{
int tmp = mtx[x][y];
int n = mtx.size();
for(int i=0; i<3; i++)
{
rot(x, y, mtx);
int _x = n - 1 - y;
int _y = x;
mtx[x][y] = mtx[_x][_y];
x = _x;
y = _y
}
mtx[x][y] = mtx[x][y];
}
void rotate( vector<vector<int> > &mtx)
{
int n=mtx.size();
if(n<=1) return;
for(int i=0; i<(n+1)/2; i++)
{
if(mtx[i].size!=n) return;
for(int j=i; j<n-i; j++)
{
rot(i, j);
}
}
}