Successed in local, but WA in oj.


  • 0
    H

    The input is:-2147483648 -1
    output is:0
    expected:2147483647

    I am really sure it was right in my laptop(at least this test case was right).I tried many many times.
    I use the g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
    below is my code:

    int divide(int dividend, int divisor) {
    	long long ans=0;
    	if(divisor==0) return INT_MAX;
    	if(dividend==0) return 0;
       	if(divisor==INT_MIN) return 0;
    	int a = (dividend > 0) ? 1:-1;
    	int b = (divisor > 0) ? 1:-1;
    	long long dividend_l = abs(dividend);
    	long long divisor_l = abs(divisor);
    	int k=0;
    	long long base = divisor_l;
    	while(dividend_l >= base){
    		base = base<<1;
    		k++;
    	}
    	for (int i = k-1; i >= 0; i--)
    	{
    		long long temp = divisor_l<<i;
    		if(temp<=dividend_l) {
    			ans += (long long)1<<i;
    			dividend_l = dividend_l-temp;
    		}
    	}
    	ans = a*b*ans;
    	return ans=(ans > INT_MAX ) ? INT_MAX : ans;
    }

Log in to reply
 

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