Tuesday, December 15, 2015

g 家面经

http://www.mitbbs.com/article_t/JobHunting/33110511.html

发信人: henhaode (henhaode), 信区: JobHunting
标  题: g 家面经
发信站: BBS 未名空间站 (Tue Dec 15 16:09:09 2015, 美东)

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). 和面试官讨
论,他没有反对,不过最后估计还是挂在了这个题上

大家有没有更好的办法?
  

No comments:

Post a Comment