leetcode 日經貼,Cpp code -Product of Array Except Self

Product of Array Except Self

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章