Product of Array Except Self
Product of Array Except Self
Problem Description
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in $O(n)$ time and without using the division operation.
Intuition
Since product[i] should be the product of all numbers except nums[i], we could break it down into two parts left[i] and right[i] were producted of the left numbers or the right.
The final project[i] is the product of left[i] and right[i].
Algorithm
1 | class Solution { |
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 YouChen's Blog!