LeetCode 238. Product of Array Except Self

简介: 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。

v2-a3264b2e9b007c24daa0b69d76ac2c32_1440w.jpg

Description



Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].


Example:

Input: [1,2,3,4]

Output: [24,12,8,6]

Note: Please solve it without division and in O(n).


Follow up:

Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)


描述



给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。


示例:


输入: [1,2,3,4]

输出: [24,12,8,6]

说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。


进阶:

你可以在常数空间复杂度内完成这个题目吗?( 出于对空间复杂度分析的目的,输出数组不被视为额外空间。)


思路



  • 声明一个结果数组res,用于返回结果.
  • 我们首先正向遍历数组,获取当前位置左边的数的所有乘积,即


v2-adacf910fc8d7bcbf1bf8465dd781dcc_720w.jpg


也就是


v2-6f4082c0a709b679f3add924b7a5b11a_720w.jpg


  • 我们声明一个辅助变量_product用于记录当前位置右边的所有数字的乘积,于是根据定义,位置i的值为(i左边所有数的乘积)与(i右边所有数的乘积)的乘积.
  • 所以res[i]= res[i](i左边所有数的乘积)*_product(i右边所有数的乘积),然后我们更新_product *= nums[i].
# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-02-02 08:52:08
# @Last Modified by:   何睿
# @Last Modified time: 2019-02-02 09:01:08
class Solution:
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        # 声明结果数组,第一个位置(索引0)一定要初始化为1
        res = [1 for _ in range(len(nums))]
        # 辅助变量
        _product = nums[-1]
        # 正向遍历,获取当前位置左边的所有数字乘积的值
        for i in range(1, len(nums)):
            res[i] = res[i - 1] * nums[i - 1]
        # 反向遍历,_product表示当前位置右边的数所有乘积的值
        for i in range(len(nums) - 2, -1, -1):
            # 当前位置的值等与当前位置左右两边所有数的乘积
            res[i] = _product * res[i]
            _product *= nums[i]
        # 返回结果
        return res

源代码文件在这里.


目录
相关文章
|
10月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
37 0
|
算法 Python
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
131 0
LeetCode 410. Split Array Largest Sum
|
算法
LeetCode 330. Patching Array
给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。
66 0
LeetCode 330. Patching Array
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
|
人工智能 索引
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 1013. 将数组分成和相等的三个部分 Partition Array Into Three Parts With Equal Sum
LeetCode 189. 旋转数组 Rotate Array
LeetCode 189. 旋转数组 Rotate Array
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
38 6
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
63 2