Lintcode 50. The product of an array after rejecting an element

Source: Internet
Author: User
Tags lintcode

Topic:

Given an array of integers a.

Define B[i] = a[0] * ... * a[i-1] * a[i+1] * ... * a[n-1], please do not use division when calculating B.

Sample Example

Given a=[1, 2, 3], return b for [6, 3, 2]

Solution: See sample, B[0]=a[1]*a[2],b[1]=a[0]*a[2],b[2]=a[0]*a[1];

Left in the code to save the value,

Right to save the value

classSolution { Public:    /** @param nums:given an integers array A * @return: a long Long array B and b[i]= a[0] * ... * a[i-1] * a[i+1 ] * ... * a[n-1]*/Vector<Long Long> productexcludeitself (vector<int> &nums) {        //Write your code here       intn =nums.size (), I; Vector<Long Long> Left (n,1); Vector<Long Long> Right (N,1); Vector<Long Long> Res (n,1);  for(i =1; I < n; ++i) left[i]= left[i-1] * nums[i-1];  for(i = n2; I >=0; --i) right[i]= right[i+1] * nums[i+1];  for(i =0; I! = Nums.size (); ++i) res[i]= Left[i] *Right[i]; returnRes; }};

Lintcode 50. The product of an array after rejecting an element

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.