LeetCode: Product of Array Except Self

https://leetcode.com/problems/product-of-array-except-self/description/

 

解题思路:

http://blog.csdn.net/wzy_1988/article/details/46916179

 

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        vector<int> result(nums.size());
        result[0] = 1;
        int p = 1;
        for (int i = 1; i < nums.size(); i++){
            p *= nums[i-1];
            result[i] = p;
        }
        p = 1;
        for (int i = nums.size()-2; i >= 0; i--){
            p *= nums[i+1];
            result[i] *= p;
        }
        return result;
    }
};

  

posted on 2017-09-02 17:08  月下之风  阅读(111)  评论(0编辑  收藏  举报

导航