careerCup1.6(没有do in place)
来源:互联网 发布:mac手写连续 编辑:程序博客网 时间:2024/04/29 13:05
//Given an image represented by an NxN matrix, where each pixel in the image is 4
//bytes, write a method to rotate the image by 90 degrees Can you do this in place?
//int 4 byte!!!
#include<iostream>
#include<string>
using namespace std;
#define N 10
struct matrix
{
int pixel[N][N];//declaration
};
//1 int 4 bytes
//2 layer,先编出基本的(最边上),然后用变量代替
//3 from corner
void rotateInPlace(matrix &m)
{
int top;
int first;
int last;
int offset;
for(int layer=0;layer<N/2;layer++)
{
first=layer;
last= N-1-first;
for(int i=first;i<last;i++)
{
offset=i-first;
//分四步
top=m.pixel[first+offset][first];
m.pixel[first+offset][first]=m.pixel[last][i];
m.pixel[last][i]=m.pixel[last-offset][last];
m.pixel[last-offset][last]=m.pixel[first][last-offset];
m.pixel[first][last-offset]=top;
}
}
}
void printMatrix(matrix m)
{ for(int j=0;j<N;j++)
{
for(int i=0;i<N;i++)
{
cout<<m.pixel[j][i]<<" ";
}
cout<<endl;
}
}
void main()
{
matrix *m=new matrix;
for(int j=0;j<N;j++)
{
for(int i=0;i<N;i++)
{
m->pixel[j][i]=j*10+i;
}
}
printMatrix(*m);
rotateInPlace(*m);
cout<<"after rotating once:"<<endl;
printMatrix(*m);
rotateInPlace(*m);
cout<<"after rotating twice:"<<endl;
printMatrix(*m);
}
//学习到的 1 in place 是不开辟新的空间
// 2 int就是4个 byte
// 3 rotate的时候先first last offset i定好,然后从特殊的开始,行列感觉走 比如一列就是[first][last-offset]
//bytes, write a method to rotate the image by 90 degrees Can you do this in place?
//int 4 byte!!!
#include<iostream>
#include<string>
using namespace std;
#define N 10
struct matrix
{
int pixel[N][N];//declaration
};
//1 int 4 bytes
//2 layer,先编出基本的(最边上),然后用变量代替
//3 from corner
void rotateInPlace(matrix &m)
{
int top;
int first;
int last;
int offset;
for(int layer=0;layer<N/2;layer++)
{
first=layer;
last= N-1-first;
for(int i=first;i<last;i++)
{
offset=i-first;
//分四步
top=m.pixel[first+offset][first];
m.pixel[first+offset][first]=m.pixel[last][i];
m.pixel[last][i]=m.pixel[last-offset][last];
m.pixel[last-offset][last]=m.pixel[first][last-offset];
m.pixel[first][last-offset]=top;
}
}
}
void printMatrix(matrix m)
{ for(int j=0;j<N;j++)
{
for(int i=0;i<N;i++)
{
cout<<m.pixel[j][i]<<" ";
}
cout<<endl;
}
}
void main()
{
matrix *m=new matrix;
for(int j=0;j<N;j++)
{
for(int i=0;i<N;i++)
{
m->pixel[j][i]=j*10+i;
}
}
printMatrix(*m);
rotateInPlace(*m);
cout<<"after rotating once:"<<endl;
printMatrix(*m);
rotateInPlace(*m);
cout<<"after rotating twice:"<<endl;
printMatrix(*m);
}
//学习到的 1 in place 是不开辟新的空间
// 2 int就是4个 byte
// 3 rotate的时候先first last offset i定好,然后从特殊的开始,行列感觉走 比如一列就是[first][last-offset]
- careerCup1.6(没有do in place)
- CareerCup1.6
- careercup1
- in-place
- Execute in place
- in-place merge
- in-place edit
- XIP - eXecute In Place
- In-place Counting Sort
- Parallel In-Place Merge
- jquery-in-place-editor
- RANDOMIZE IN PLACE
- Raspberry Pi in place
- Algorithm: Inverse in place
- in-place数据交换
- 【OpenCV】in-place操作
- in-place操作
- Rotating an array in place
- 你所不知道的提高spring+hibernate性能的一个方法---Mysql
- mysql的表名/字段名/字段值是否区分大小写
- Objective-C基础概要
- hdu 4034 Graph解题报告-Floyd思想
- sap basis 常用事务代码
- careerCup1.6(没有do in place)
- 三、调用者的实现
- 如何使用 RamFS 与 TempFS
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- 三维几何变换
- 日本最大的网络购物公司乐天宣告败北
- Ext.tree.TreePanel
- 执行Oracle中的sqlldr xxx.ctl命令导入数据时,当错误无法导入时,注意查看「xxx.log信息」
- 接下来一个月工作安排