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