g 家面经

原帖地址:mitbbs

you have an img data as an array, output the data for upsampling. For
example,
[1, 2, 3, 4, 5, 6] as width 3(2 rows) ==> upsample 2 times would be [1 1 2 2
3 3 1 1 2 2 3 3 4 4 5 5 6 6 4 4 5 5 6 6]

int[] UpSampling(int[] input, int width, int times)
{
//check the validation of input parameters

int numRows = input.Length / width;
int[] result = new int[input.Length * Math.Pow(times, 2)];
int index = 0;

for(int i = 0; i < numRows; i++) { for(int j = 0; j < times; j++) { for(int k = 0; k < width; k++) { for(int l = 0; l < times; l++) { result[index++] = input[i*width + k]; } } } } return result; } 我这个算法用了4 个 loop, 看起来有点笨, 不过performance还是o(n). 和面试官讨 论,他没有反对,不过最后估计还是挂在了这个题上 大家有没有更好的办法?