lintcode-easy-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.

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

public class Solution {
    /**
     * @param A: Given an integers array A
     * @return: A Long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
     */
    public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) {
        // write your code
        ArrayList<Long> result = new ArrayList<Long>();
        if(A == null || A.size() == 0)
            return result;

        long[] left2curr = new long[A.size()];
        long[] curr2right = new long[A.size()];

        left2curr[0] = 1;
        curr2right[A.size() - 1] = 1;

        for(int i = 1; i < A.size(); i++)
            left2curr[i] = A.get(i - 1) * left2curr[i - 1];
        for(int i = A.size() - 2; i >= 0; i--)
            curr2right[i] = A.get(i + 1) * curr2right[i + 1];

        for(int i = 0; i < A.size(); i++)
            result.add(left2curr[i] * curr2right[i]);

        return result;
    }
}
时间: 2024-11-03 21:10:39

lintcode-easy-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

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 Note There may exist multiple valid solutions, return any of them. ////////////////////// 二叉查

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 * @para

[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

【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

#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 [