Friday, September 28, 2012

[LeetCode] Divide two integers

Divide two integers without using multiplication, division and mod operator.

解题思路:
   1. 类似二分法:每次左移divisor 一位。
   2.  用log(a-b)=loga/logb
注意:overflow, underflow, 并且要考虑符号问题
 
public int divide(int dividend, int divisor) {
         if(dividend == 0 || divisor == 1) return dividend;
        
        int sign = 1;
        
        if(dividend < 0) sign *= -1;
        if(divisor < 0) sign *= -1;
        
        long dvd = Math.abs((long)dividend); //the overflow and underflow
        long dvs = Math.abs((long)divisor);
        int result = 0;
        int i = 0;
        
        while(dvd >= dvs){
            dvs <<=1;
            i++;
        }
       
        while(dvd < dvs && i > 0){
              i--;
              dvs >>= 1;
              
              if( dvd >= dvs ){
                  result += 1 << i;
                  dvd -= dvs;
              }
              
        }
                
        return sign*result;
        
    } 
 
以下为log的方法,由于在java里,double并不是准确的值,有一定的误差。所以最后的结果要有一个误差的调整。 
 
 public int divide(int dividend, int divisor) {
        if(dividend == 0 || divisor == 1) return dividend;
        
        int sign = 1;
        
        if(dividend < 0) sign *= -1;
        if(divisor < 0) sign *= -1;
        
         if(dividend==Integer.MAX_VALUE&&divisor==Integer.MIN_VALUE) return 0;
         else if(dividend==Integer.MIN_VALUE&&divisor==Integer.MAX_VALUE) return sign < 0? -1:1;
    
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        
        double t = Math.log10(dividend) - Math.log10(divisor);
        double ret = Math.pow(10, t);
        int result = (int) Math.floor(ret+ 0.0000001);
        
        return sign*result;
    }

No comments:

Post a Comment