C++ with no extra space. Not even return vector. Overall beats 60%


  • -1
    H

    '''
    class Solution {
    public:
    vector<int> productExceptSelf(vector<int>& nums) {
    int prod=1;
    int count=0;
    for(auto c:nums){
    if(c==0){count++;}
    else {prod = c*prod ;}
    }
    for(register int i = 0 ; i < nums.size();++i){
    if(nums[i]==0 && count==1)nums[i]=prod;
    else if(nums[i]==0 && count>1)nums[i]=0;
    else if (count>=1) nums[i]=0;
    else nums[i]=prod/nums[i];
    }
    return nums;
    }
    };
    '''


Log in to reply
 

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