2ms Java recursive Solution


  • 1
    M
    public int divide(int i_dividend, int i_divisor) {
        int sign = 1;
        long dividend = (long)i_dividend;
        long divisor = (long)i_divisor;
        
        if(dividend<0){
            dividend = -dividend;
            sign = -sign;
        }
        
        if(divisor<0){
            divisor = -divisor;
            sign = -sign;
        }
        
        long cnt = helper(dividend, divisor);
        
        cnt = sign>0?cnt:-cnt;
        
        if(cnt>Integer.MAX_VALUE) return Integer.MAX_VALUE;
        else if(cnt< Integer.MIN_VALUE) return Integer.MIN_VALUE;
        else
            return (int)cnt;
    }
    private long helper(long dividend, long divisor){
        if(dividend<divisor) return 0;
        long origin = divisor;
        long cnt =1;
        while(divisor<<1 < dividend){
            divisor <<=1;
            cnt <<=1;
        }
        if(divisor == dividend) return cnt;
        else{
            cnt+=helper(dividend-divisor, origin);
        }
        return cnt;
    }

Log in to reply
 

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