My stupid solution


  • 0
    L

    if there is 0 in nums (nums[i] = 0 ) , we can inferr that other members are 0 , so we only need traverse once to work out the value of nums[i]
    if no 0 ,traverse twice

    '''

    vector<int> productExceptSelf(vector<int>& nums) {
        int res = 1 , tmp = 1;
        for(int i = 0 ; i < nums.size() ; ++i){
            if(nums[i] == 0){
                for(int j = 0 ; j < nums.size() ; ++j){                    
                    if(j != i)tmp *= nums[j];
                    nums[j] = 0;
                }
                nums[i] = tmp;
                return nums;
            }
            else res *= nums[i];  
        }
        for(int i = 0 ; i < nums.size() ; ++i)
            nums[i] = res / nums[i];
    
        return nums;
    }
    

    ''''


Log in to reply
 

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