Redian新闻
>
BB那个trade-in的coupon能用来买surface吗?
avatar
BB那个trade-in的coupon能用来买surface吗?# PDA - 掌中宝
g*s
1
rotate 一个 n*n的bitmap 90度。
我写的是逐行扫或者inplace的四角扫,复杂度都是n^2吧? 也就是说至少每个元素要
遍历一次。可是面试官说有更好的方法。并提示我这是bitmap,比如每个pixel可以存
8bit的value。
最后还是没想出来有什么跟币bitmap优化的方法。。。。。大家能提示下么?
顺带求解的bug, 谁能从原理上分析下为甚下面的code会crash么?
class A
{
public:
~A(){cout<};
class B : public A
{
public:
virtual ~B(){cout<};

int main()
{
A* b = new B();
delete b;
return 0;
}
avatar
A*r
2
好像不能买tablet,不过surface pro也不完全算tablet吧?
avatar
r*h
3
因为A的析构函数没有定义成虚函数
从而A类指针指向B类时调用析构函数找到的是A析构函数的入口地址,B的析构函数未被
执行

【在 g*******s 的大作中提到】
: rotate 一个 n*n的bitmap 90度。
: 我写的是逐行扫或者inplace的四角扫,复杂度都是n^2吧? 也就是说至少每个元素要
: 遍历一次。可是面试官说有更好的方法。并提示我这是bitmap,比如每个pixel可以存
: 8bit的value。
: 最后还是没想出来有什么跟币bitmap优化的方法。。。。。大家能提示下么?
: 顺带求解的bug, 谁能从原理上分析下为甚下面的code会crash么?
: class A
: {
: public:
: ~A(){cout<
avatar
w*i
4
同问
avatar
e*i
5
Need to make base class destructor virtual. Here since A's destructor is
nonvirtual, "delete b" will call A's destructor. But b is actually pointing
to a B object. It will free memory of sizeof(A), not sizeof(B), thus crash.
avatar
p*3
6
什么叫更好的
是不是你用了O(n)空间
如果已经是O(1)空间的话那不是时间上要小于O(n^2)? 那应该是不可能吧

【在 g*******s 的大作中提到】
: rotate 一个 n*n的bitmap 90度。
: 我写的是逐行扫或者inplace的四角扫,复杂度都是n^2吧? 也就是说至少每个元素要
: 遍历一次。可是面试官说有更好的方法。并提示我这是bitmap,比如每个pixel可以存
: 8bit的value。
: 最后还是没想出来有什么跟币bitmap优化的方法。。。。。大家能提示下么?
: 顺带求解的bug, 谁能从原理上分析下为甚下面的code会crash么?
: class A
: {
: public:
: ~A(){cout<
avatar
g*s
7
用四角inplace方法是不用额外空间的。
我也很郁闷,跟他说再怎么每个元素也至少要visit一遍啊? 他说不一定。并提示2d
matrix在memory里是1D的,但我还是没想出来怎么搞。。。。。

【在 p****3 的大作中提到】
: 什么叫更好的
: 是不是你用了O(n)空间
: 如果已经是O(1)空间的话那不是时间上要小于O(n^2)? 那应该是不可能吧

avatar
g*G
10
这个其实不偏,貌似我是在大概五六年前看过的一个优化。。
也算是计算机体系结构这块的积累吧

【在 g*******s 的大作中提到】
: 多谢了!看来这个就是面试官想要的。不过看到这个我也死心了,压根没往硬件架构和
: cache优化的思路想。只关心理论复杂度了。。。。。

avatar
g*s
11
说的是,这也算是几个loop优化的基本方法之一。唉,最近光做题了。。。。一看到
leetcode上的原题就思维定式了。。。。还是平时要多积累

【在 g**G 的大作中提到】
: 这个其实不偏,貌似我是在大概五六年前看过的一个优化。。
: 也算是计算机体系结构这块的积累吧

avatar
r*n
12
其实还有一个快速解法,虽然不是inplace,不用swap,而是基于copy:
A 2-D matrix can be interpreted as a 1-D array in two different ways:
the (i,j) entry is given by
arr[i*N+j] (concatenating rows)
or
arr[i+j*N] (concatenating columns)
suppose the given 2-D matrix is represented in the first form, you can
generate a rotated matrix expressed in the second form:
for(int k = N^2-1; k >= 0; k--){
arr_new[N-1-k/N+k%N] = arr[k];
}
then just copy back to the original array
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
arr[i*N+j] = arr_new[i+j*N];
}
}
avatar
r*n
13
在最后复制回去的时候貌似还是很多cache miss,不过如果不需要还原成原来的方式,
应该还是不错的。

【在 r*********n 的大作中提到】
: 其实还有一个快速解法,虽然不是inplace,不用swap,而是基于copy:
: A 2-D matrix can be interpreted as a 1-D array in two different ways:
: the (i,j) entry is given by
: arr[i*N+j] (concatenating rows)
: or
: arr[i+j*N] (concatenating columns)
: suppose the given 2-D matrix is represented in the first form, you can
: generate a rotated matrix expressed in the second form:
: for(int k = N^2-1; k >= 0; k--){
: arr_new[N-1-k/N+k%N] = arr[k];

avatar
e*i
14
It is essentially to convert a C 2D array to a Fortran 2D array.
Hard to believe this method will be faster than in-place swapping method.
Its (N-1-k/N+k%N) index conversion and extra copy-back will kill performance.
DaNiuMen have any better solutions?

: the (i,j) entry is given by
: arr[i*N+j] (concatenating rows)
: or
: arr[i+j*N] (concatenating columns)
: suppose the given 2-D matrix is represented in the first form, you can
: generate a rotated matrix expressed in the second form:
: for(int k = N^2-1; k >= 0; k--){
: arr_new[N-1-k/N+k%N] = arr[k];
: ...................

【在 r*********n 的大作中提到】
: 其实还有一个快速解法,虽然不是inplace,不用swap,而是基于copy:
: A 2-D matrix can be interpreted as a 1-D array in two different ways:
: the (i,j) entry is given by
: arr[i*N+j] (concatenating rows)
: or
: arr[i+j*N] (concatenating columns)
: suppose the given 2-D matrix is represented in the first form, you can
: generate a rotated matrix expressed in the second form:
: for(int k = N^2-1; k >= 0; k--){
: arr_new[N-1-k/N+k%N] = arr[k];

avatar
r*n
15

this part shouldn't be the bottleneck since the cache miss happens at most N
times (integer arithmetic shouldn't be a problem)
and extra copy-back will kill performance.
i was originally thinking about using the 2D array in the second form. in
that case you don't need to convert it back to the first form.
in fact when you swap, normally you still copy it into a temporary variable
and then copy back, unless you use xor-swap, but that is also not free.

【在 e*******i 的大作中提到】
: It is essentially to convert a C 2D array to a Fortran 2D array.
: Hard to believe this method will be faster than in-place swapping method.
: Its (N-1-k/N+k%N) index conversion and extra copy-back will kill performance.
: DaNiuMen have any better solutions?
:
: : the (i,j) entry is given by
: : arr[i*N+j] (concatenating rows)
: : or
: : arr[i+j*N] (concatenating columns)
: : suppose the given 2-D matrix is represented in the first form, you can

avatar
g*s
16
简单试了下非inplace方法用tiling和不用的,用了tiling确实快了不少,4000×4000
以上的matrix,用size 8的tiling至少快3倍左右。
void rotate1 (int ** in, int ** out, int n)
{
for(int i=0; ifor(int j=0; jout[i][j]=in[n-j-1][i];
}
void rotate2 (int ** in, int ** out, int n, int block)
{
for (int x=0; xfor(int y=0; yfor(int i=0; ifor(int j=0; jout[i+x][j+y]=in[n-y-j-1][i+x];
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。