`

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

我们创建一个长度和原数组num[]长度一样的数组result[],先从左边开始,让result[0] = 1; result[1] = 1 * num[0], result[2] = num[0] * num[1], result[n] = num[0] * num[1] ...num[n-1]。然后从右边开始,让result[n] = result[n] * 1; result[n - 1] = num[n] * result[n - 1]; result[0] = num[n] * num[n - 1] .... num[1] * result[0]。这样遍历两遍数组就可以得到结果,代码如下:
public class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];
        int left = 1;
        result[0] = 1;
        for(int i = 1; i < nums.length; i++) {
            result[i] = left * nums[i - 1];
            left *= nums[i - 1];
        }
        int right = 1;
        for(int j = nums.length - 1; j >= 0; j--) {
            result[j] *= right;
            right *= nums[j];
        }
        return result;
    }
}
分享到:
评论

相关推荐

    Leetcode 238. Product of Array Except Self

    思路:题目中说不能用除法;用暴力,会超时。 为了计算 除第i个元素的乘积,可以将数组分为三个部分:1…i-1; i; i+1…n (注:这里的i代表序号,不是数组下标)  output[i] = a * b[被乘数a:代表前i-1个数的乘积...

    python-leetcode题解之238-Product-of-Array-Except-Self.py

    python python_leetcode题解之238_Product_of_Array_Except_Self.py

    戳气球leetcode-leetcode:leetcode

    Product of Array Except Self 189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All ...

    算法刷题笔记leetcode/lintcode

    - Product of Array Except Self(数组中除了自身以外的乘积) - Partition Array(分割数组) - First Missing Positive(缺失的第一个正数) - 2 Sum(两数之和) - 3 Sum(三数之和) - 3 Sum Closest(三...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 ...of ...of ...Array ...Product of Array Except Self 16 Valid Parenthesis String 17 Number of Islands 18 M

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava ...Product of Array Except Self #242 Valid Anagram #258 Add Digits #260 Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P

    Leetcode部分试题解析

    13. **Product of Array Except Self**:不包含自身的数组乘积。可以使用前缀和和后缀乘积的概念,结合位运算优化计算。 14. **Invert Binary Tree**:翻转二叉树。这可以通过递归实现,每个节点的左子树和右子树...

    lrucacheleetcode-leetcode:记录自己的leetcode解题历程~Welcomeeveryonetocomment:grinning_face:~

    Product of Array Except Self 697. Degree of an Array 849. Maximize Distance to Closest Person ————————————————————————————————————————————————

    LeetCode最全代码

    318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-...

    LeetCodeTrain:这是来自LeetCode的已解决任务的存储库

    这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 ...//leetcode....

    EhLib5.0.13 最新的ehlib源码

    of TPrinter except some details. In TPrinter Printer.Canvas.Handle and Printer.Handle is the same but in TPrinterPreview PrinterPreview.Canvas.Handle represent the metafile in that is recored the ...

Global site tag (gtag.js) - Google Analytics