My c++ solution


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

  • -1
    Y

    you have lost a * symbol in the line : result[i] = before;


  • 0
    K

    Actually, I intended to omit that operation. Think twice, please.


Log in to reply
 

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