O(1)space,O(n)time


  • 0
    S

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


Log in to reply
 

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