Applovin OA 面试真题解析:Product of Array Except Self 数组前缀后缀乘积

14次阅读
没有评论

Given an input array, return a result array where each element at index i is the product of all values in the input array except input[i].

Example input array:

{2, 4, 3, 5}

Example result array:

{60, 30, 40, 24}
  • 60 is the product of all values of the input array except 2.
  • 30 is the product of all values of the input array except 4.
  • 40 is the product of all values of the input array except 3.
  • 24 is the product of all values of the input array except 5.

这是一道经典的“数组中除自身以外元素的乘积”题目。对于输入数组 {2, 4, 3, 5},结果数组分别对应“除了当前位置元素之外,其余所有元素的乘积”,因此输出为 {60, 30, 40, 24}。这类题的常见思路是用前缀乘积和后缀乘积分别计算每个位置左边与右边的乘积,再合并得到答案,从而避免使用除法,也能把时间复杂度控制在 O(n)。

正文完
 0