Product of Array Exclude Itself

Given an integers array A.

Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.

Example

For A = [1, 2, 3], return [6, 3, 2].

分析:

其实这题的限制条件挺脑残的,但是感觉实现的方式还是可以用在其它地方的。

 1 public class Solution {
 2     /**
 3      * @param A: Given an integers array A
 4      * @return: A Long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) {
 8          ArrayList<Long> result = new ArrayList<Long>();
 9         if (A.size() <= 1) {
10             result.add((long)1);
11             return result;
12         }
13         long[] left = new long[A.size()];
14         long[] right = new long[A.size()];
15         left[0] = 1;
16         for (int i = 1; i < A.size(); i++) {
17             left[i] = left[i-1] * A.get(i-1);
18         }
19         right[A.size()-1] = 1;
20         for (int i = A.size()-2; i >= 0; i--) {
21             right[i] = right[i+1] * A.get(i+1);
22         }
23         for (int i = 0; i < A.size(); i++) {
24             result.add(left[i] * right[i]);
25         }
26         return result;
27     }
28 }

转载请注明出处:cnblogs.com/beiyeqingteng/

时间: 2024-10-11 02:51:09

Product of Array Exclude Itself的相关文章

lintcode 容易题:Product of Array Exclude Itself 数组剔除元素后的乘积

题目: 数组剔除元素后的乘积 给定一个整数数组A. 定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法. 样例 给出A=[1, 2, 3],返回 B为[6, 3, 2] 解题: leftb计算左侧的连乘值,每次增加一个成绩,rightb计算右侧的连乘值,每次重新计算,时间复杂度O(N2),下面写的程序,自我感觉很差的... Java程序: public class Solution { /** * @param

【08_238】Product of Array Except Self

Product of Array Except Self Total Accepted: 26470 Total Submissions: 66930 Difficulty: Medium Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of numsexcept nums[i

【LeetCode】Product of Array Except Self

Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2

[LintCode] Product of Array Except Self 除本身之外的数组之积

Given an integers array A. Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation. Have you met this question in a real interview? Yes Example For A = [1, 2, 3], return [6, 3, 2]. LeetCode上的原题,请参见我之前的博客Product

#leetcode#Product of Array Except Self

Product of Array Except Self Total Accepted: 442 Total Submissions: 1138My Submissions Question  Solution Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums e

LeetCode Q238 Product of Array Except Self(Medium)

Product of Array Except Self 原题: Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n). For example, given [

Leetcode 238. Product of Array Except Self

238. Product of Array Except Self Total Accepted: 51070           Total Submissions: 116543           Difficulty: Medium Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the ele

[leedcode 238] Product of Array Except Self

Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Fo

LeetCode OJ 238. Product of Array Except Self 解题报告

题目链接:https://leetcode.com/problems/product-of-array-except-self/ 238. Product of Array Except Self My Submissions Question Total Accepted: 36393 Total Submissions: 87262 Difficulty: Medium Given an array of n integers where n > 1, nums, return an arr