[Leetcode] 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.)

Analysis: The topic of translation: the need to calculate an array, in addition to the product of all elements other than itself, can not use division.      Looking at the requirements, without looking at the limit, we have two ideas: 1. Calculate the product of all the elements, then traverse from beginning to end, for any i,res[i] = product/nums[i]. This is, of course, the most general approach, but it is not possible to use division, and this method is ignored. 2. Join us to calculate the results of I position res[i], is actually calculated res[i]=nums[0]*nums[1]*...*nums[i-1]*nums[i+1]*...*nums[nums.length-1] The following is metaphysics: Res[i] =nums[0]*nums[1]*...*nums[i-1]*nums[i+1]*...*nums[nums.length-1]= (Nums[0]*nums[1]*...*nums[i-1]) * (nums[i+1]*     *NUMS[NUMS.LENGTH-1]) that is to divide the result into two parts, left and right, so res[i]=left[i]*right[i], here is how to determine the left and right arrays. Left[i]=nums[0]*nums[1]*...*nums[i-1], this is a recursion. , Left[i]=left[i-1]*nums[i-1] code is as follows:
1 classSolution {2      Public int[] Productexceptself (int[] nums) {3         intn =nums.length;4 5         int[] left =New int[n];6Left[0] = 1;7          for(inti = 1; I < n; i + + )8Left[i] = left[i-1] * nums[i-1];9 Ten         int[] right =New int[n]; OneRight[n-1] = 1; A          for(inti = n-2; I >= 0; I-- ) -Right[i] = right[i+1] * nums[i+1]; -          the         int[] res =New int[n]; -          for(inti = 0; I < n; i + + ) -Res[i] = left[i] *Right[i]; -          +         returnRes; -     } +}

Run time 1ms, beat 100%.

But this method still uses the extra space, the topic said hoped can not use the extra space. Therefore, you can use a right variable instead of the one that is left.

Refer to the code of the discuss great God:

1  Public int[] Productexceptself (int[] nums) {2     intn =nums.length;3     int[] res =New int[n];4Res[0] = 1;5      for(inti = 1; I < n; i++) {6Res[i] = res[i-1] * nums[i-1];7     }8     intright = 1;9      for(inti = n-1; I >= 0; i--) {TenRes[i] *=Right ; OneRight *=Nums[i]; A     } -     returnRes; -}

[Leetcode] 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.