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

Follow up:

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

思路

这里写图片描述

对于i = 5 时 result[5] = (nums[0] * nums[1] * nums[2] * nums[3] * nums[4] ) * (nums[6] nums[7] * nums[8] * nums[9] * nums[10])

从上面开始看出对于第i个,我们只要知道它左边的连续乘积它右边的连续乘积 就OK了。

代码

/*---------------------------------------
*   日期:2015-07-31
*   作者:SJF0115
*   题目: 238.Product of Array Except Self
*   网址:https://leetcode.com/problems/product-of-array-except-self/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int size = nums.size();
        vector<int> result(size,0);
        vector<int> left(size,0);
        vector<int> right(size,0);
        int leftNum = 1,rightNum = 1;
        // left[i] 为 nums[0]...nums[i-1]的连续乘积
        // right[i] 为 nums[i+1]...nums[size-1]的连续乘积
        for(int i = 0;i < size;++i){
            left[i] = leftNum;
            right[size-i-1] = rightNum;
            leftNum *= nums[i];
            rightNum *= nums[size-i-1];
        }//for
        // 计算不包括自己的所有乘积
        for(int i = 0;i < size;++i){
            result[i] = left[i] * right[i];
        }//for
        return result;
    }
};

int main(){
    Solution s;
    vector<int> vec = {1,2};
    vector<int> result = s.productExceptSelf(vec);
    int size = result.size();
    for(int i = 0;i < size;++i){
        cout<<result[i]<<" ";
    }//for
    return 0;
}

运行时间

这里写图片描述

目录
相关文章
|
6月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
21 0
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
|
算法 Python
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
108 0
LeetCode 410. Split Array Largest Sum
|
算法
LeetCode 330. Patching Array
给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。
50 0
LeetCode 330. Patching Array
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
75 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 238. Product of Array Except Self
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。
62 0
LeetCode 238. Product of Array Except Self