C++ O(n)


  • 0

    C++ version of this thread.

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.