C++ solution thanks to your tips


  • 1
    A
    class Solution {
    public:
        vector<int> productExceptSelf(vector<int>& nums) {
            // use two rounds to get the result: the 1st round for multiplying every element on the left side of current position, the 2nd round for multiplying every element on the right side of current position
            int length = nums.size();
            vector<int> ans(length,1);
            int leftSideProduct = 1;
            for (int i=0;i<length;i++) { 
                ans[i] = leftSideProduct; // leftSideProduct is the product of all the elements on the left side of position i
                leftSideProduct = leftSideProduct * nums[i]; // update leftSideProduct and keep going right
            }
            int rightSideProduct = 1;
            for (int j=length-1;j>=0;j--) {
                ans[j] = ans[j] * rightSideProduct; // rightSideProduct is the product of all the elements on the right side of position j
                rightSideProduct = rightSideProduct * nums[j]; // update rightSideProduct and keep going left
            }
            return ans;
        }
    };

Log in to reply
 

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