C++ check zero...very easy understanding. need to simplify somehow


  • 0
    class Solution {
    public:
        vector<int> productExceptSelf(vector<int>& nums) { 
            int totalMultiple =1;
            int zeroTime=0, position=0;
            for(int i=0; i<nums.size(); i++)
            {
                if(nums[i]==0) 
                { 
                    zeroTime++; 
                    if(zeroTime>1) break; 
                    position = i;
                }
                else
                    totalMultiple*=nums[i];
            }
            vector<int> result(nums.size(), 0);
            if(zeroTime == 1)
                result[position] = totalMultiple;
            else if(zeroTime == 0)
            {
                for(int i=0; i<nums.size(); i++)
                    result[i] = totalMultiple/nums[i];
            }
            return result;
        }
    };
    

Log in to reply
 

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