60ms C++ solution


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

  • 0
    A

    Wow. Interesting solution... Simple, but a little bit difficult for newbies :D


Log in to reply
 

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