`
SunnyYoona
  • 浏览: 384925 次
社区版块
存档分类
最新评论

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

运行时间

这里写图片描述

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
分享到:
评论

相关推荐

    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

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

    lru缓存leetcode 我们使用python来处理关于leetcode和linkcode的话题。 leetcode 网址 = 链接代码网址 = 大致分为几个主题: ————————————————————————————————————————...

    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语言解决任务 CoinChange.java - //leetcode....

    戳气球leetcode-leetcode:leetcode

    leetcode category other hot keywords:Palindrome(mic), Subsequence Array 螺旋矩阵Spiral Matrix 顺时针打印矩阵 Next Permutation Product of Array Except Self 189.rotate-array 283.move-zero Range Sum ...

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

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    Leetcode部分试题解析

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

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...

    算法刷题笔记leetcode/lintcode

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

Global site tag (gtag.js) - Google Analytics