Share my C++ solution with constant space usage


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

Log in to reply
 

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