C++ solution accepted


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

Log in to reply
 

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