Why I am getting TLE?


  • 0
    S

    int divide(int dividend, int divisor) {
    if(divisor==0) return INT_MAX;
    if(divisor==1) return dividend;
    if(divisor==-1) return 0-dividend;
    int ans=0,n;
    if((dividend<divisor && (dividend>0 && divisor>0))|| dividend==0) return ans;
    bool plus=true;
    if(dividend<0 && divisor>0) {dividend=0-dividend;plus=false;}
    if(dividend>0 && divisor<0) {divisor=0-divisor;plus=false;}
    if(dividend<0 && divisor<0) {dividend=0-dividend; divisor=0-divisor;}
    ans=1; dividend=dividend-divisor;
    n=divisor;
    //cout<<n<<endl; return 0;
    while(1)
    {
    //if(n+n<dividend){cout<<"\nn is:"<<n<<endl;ans<<=1; n=n+n;}
    //else {dividend=dividend-n; cout<<"ans is:"<<ans<<endl;break;}
    if(n+n>dividend){if(ans!=1){dividend=dividend-n; ans++;/cout<<"ans is:"<<ans<<endl;/}break;}
    else {/cout<<"\nn is:"<<n<<endl;/if(n+n>0){ans<<=1; n=n+n;} else{ if(ans!=1){dividend=dividend-n; ans++;}break;}}
    }
    //cout<<"dividend and ans:"<<dividend<<" "<<ans<<endl;
    while(dividend>=divisor)
    {
    dividend=dividend-divisor;
    ans++;
    }
    return (plus)?ans:0-ans;
    }


Log in to reply
 

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