My clean C++ code,without extra temp space


  • 1
    W
    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] = nums[i] * res[i];
            }
            for(int i = n - 1;i > 0;--i){
                res[0] *= nums[i];
                if(i - 1 == 0)  return res;
                res[i - 1] *= res[0];
            }
            return res;
        }
    };

Log in to reply
 

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