[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;
}

运行时间

时间: 2025-01-21 06:11:18

[LeetCode]238.Product of Array Except Self的相关文章

[LeetCode] Subarray Product Less Than K 子数组乘积小于K

Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8

[LeetCode] K Inverse Pairs Array K个翻转对数组

Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's a

[LeetCode] 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] Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 常规思路 class Solution { public: int maxProd

[LeetCode]88.Merge Sorted Array

[题目] Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B

[LeetCode]108.Convert Sorted Array to Binary Search Tree

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 二分法,以中间元素i为根节点[start,i-1]递归构建左子树,[i+1,end]递归构建右子树 [代码] /********************************* * 日期:2014-12-28 * 作者:SJF0115 * 题目: 108.Convert Sorte

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode]109.Convert Sorted List to Binary Search Tree

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 无 [代码] 在下面超时的代码上进行改进:把链表先转换为vector再进行操作. [LeetCode]108.Convert Sorted Array to Binary Search Tree /*******************************

通过Safari浏览器获取iOS设备UDID(设备唯一标识符)

摘要:通过苹果Safari浏览器获取iPhone UDID步骤详解:苹果公司允许开发者通过IOS设备和Web服务器之间的某个操作,来获得IOS设备的UDID(包括其他的一些参数). 通过苹果Safari浏览器获取iPhone UDID步骤详解: 一.获得UDID通过移动Safari概述: 苹果公司允许开发者通过IOS设备和Web服务器之间的某个操作,来获得IOS设备的UDID(包括其他的一些参数).这里的一个概述: 1.在你的Web服务器上创建一个.mobileconfig的XML格式的描述文件