java.lang.Object
g0201_0300.s0238_product_of_array_except_self.Solution

public class Solution extends Object
238 - Product of Array Except Self.<p>Medium</p> <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p> <p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4]</p> <p><strong>Output:</strong> [24,12,8,6]</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [-1,1,0,-3,3]</p> <p><strong>Output:</strong> [0,0,9,0,0]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= nums.length <= 10<sup>5</sup></code></li> <li><code>-30 <= nums[i] <= 30</code></li> <li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li> </ul> <p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code>extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • productExceptSelf

      public int[] productExceptSelf(int[] nums)