[Leetcode] 238. Product of Array Except self

Source: Internet
Author: User

Given an array nums of n integers where n > 1, return a array output such that's equal to output[i] th E product of all the elements of nums except nums[i] .

Example:

Input:  [1,2,3,4][24,12,8,6]

Note:please solve it without division and in O (n).

Follow up:
Could solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

Test instructions: Gives an array that outputs the product of all numbers except this number

Note that you do not need to divide (in fact you use it will not error), as far as possible in a constant number of space to solve the problem.

If you use division, you will definitely, without division, and be done under O (n) conditions.

We get to the point where the key has two points, the array and the product, and the answer to this position can be seen as the product of all the numbers on its left multiplied by the number on the right.

Then we need to open two extra arrays, from left to right, one at a time, and then one at a time.

classSolution { Public int[] Productexceptself (int[] nums) {        int[] left =New int[Nums.length]; int[] rigth =New int[Nums.length]; int[] res =New int[Nums.length]; left[0] = Nums[0]; Rigth[nums.length-1] = Nums[nums.length-1];  for(inti = 1; i < nums.length; i++) {Left[i]= left[i-1] *Nums[i]; }         for(inti = nums.length-2; I >= 0; i--) {Rigth[i]= Rigth[i + 1] *Nums[i]; }         for(inti = 1; i < nums.length-1; i++) {Res[i]= Rigth[i + 1] * left[i-1]; } res[0] = rigth[1]; Res[nums.length-1] = Left[nums.length-2]; returnRes; }}

Haha, this does not meet the constant space requirements (also can be said O (1) space requirements)

No extra space to open, so let's optimize it and record it while counting.

From right to left, we save to Res, from left to right, we use an int instead (this will not affect the unused elements in res).

classSolution { Public int[] Productexceptself (int[] nums) {        int[] res =New int[Nums.length]; Res[nums.length-1] = Nums[nums.length-1];  for(inti = nums.length-2; I >= 0; i--) {Res[i]= res[i + 1] *Nums[i]; } res[0] = res[1]; intMix = Nums[0];  for(inti = 1; i < nums.length-1; i++) {Res[i]= mix * Res[i + 1]; Mix*=Nums[i]; } res[nums.length-1] =mix; returnRes; }}

[Leetcode] 238. Product of Array Except self

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.