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;
    
    	while(1)
    	{
    		if(n+n>dividend){if(ans!=1){dividend=dividend-n; ans++;}break;}
    		else {
                                   if(n+n>0){ans<<=1; n=n+n;} 
                                else{ 
                                           if(ans!=1){dividend=dividend-n; ans++;}
                                           break;
                                       }
                                }
    	}
    
    	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.