您的位置:首页 > 编程语言 > C语言/C++

leetcode #29 in cpp

2016-05-24 02:37 483 查看
The question is to divide two integer without multiplication,mod and division. 

Solution: 

The only things we can use are + and -. We know that multiplication is actually the shortcut of addition. 2*5 = 2 + 2 + 2 + 2 + 2 or 5 + 5 = 10 --> 10/2 =5 or 10/5 = 2. Dividend/Divisor = the number of divisor for addition
to get to the dividend.   Thus the brute force way is to:

Suppose dividend is larger than divisor

int multiplier = 1;
int sum = divisor;
while(sum < dividend){
sum+= divisor;
<span style="white-space:pre"> </span>multiplier ++;
}
return multiplier;
But this is way too slow. If we have divisor as 2 and dividend as 1000000, it needs 1000000/2 iterations. So we need to speed it up, and the trick is again to use addition. Every time 'sum' tries to approach 'dividend', it takes a step of the amount
of itself instead of divisor, as well as 'count' does.  
Example: dividend = 16 and divisor = 3;
step 1: sum = 0 + 3            multiplier = 0 + 1.
(Note that 3 = 3*1)

step 2: sum = 3 + 3  = 6     multiplier = multiplier + multiplier= 2. (Note that 6 = 3*2)

step 3: sum = 6 + 6 = 12    multiplier= multiplier + multiplier = 4. (Note that 12 = 3*2*2 = 3*4)

See? The sum we record is exactly equal to count * divisor. And the approach to dividend is in the speed of exponential increase in sum and could thus reduce the
complexity to O(log n). 

However we are not done yet. If dividend = divisor * odd_multiplier or dividend is not a multiple of divisors (dividend mod divisor != 0), then this approach could not give a
correct answer. In our example 16 = 3 * 5, and step 3 gives multiplier = 4 which is not correct. 

How to solve this? The idea is that when sum is about to go larger than dividend after adding itself, say 12 + 12 = 24 > 16, we stop current exponential increase. we
regenerate an exponential increase with initial step = divisor to approach 16 from 12. That is, we do not want to take a large step and thus we restart the exponential
increase which gives us smaller increase in its beginning. In my implementation, I shift [current sum, dividend] to [0, dividend - current sum] for convenience. 

step 4: sum = 12 + 12 = 24 > 16, restart exponential increase. store final_multiplier = 4, set sum = 0, multiplier = 0, set dividend = 16 - 12 = 4. 

step 5: sum = 0 + 3           multiplier = multiplier + 1 = 1.

Step 6: sum = 3 + 3 > dividend = 4, restart exponential increase. store final_multiplier = 4 + 1 = 5. dividend = 4 - 3 = 1 < divisor, we can stop. 

One trouble in this question is the overflow problem. I list several if condition in my code to deal with thay

Code:

class Solution {
public:
    int divide(int dividend, int divisor) {
        if (divisor == 0 || (dividend == INT_MIN && divisor == -1)) {
            return INT_MAX;
        }
        
        int sign = dividend > 0 && divisor > 0 || dividend < 0 && divisor < 0?1:-1;
        
        long long int new_dividend = dividend;//
        new_dividend = new_dividend< 0 ?-new_dividend: new_dividend;//
        long long int new_divisor = divisor; //
        new_divisor = new_divisor< 0 ?-new_divisor: new_divisor;//
       
        //special cases
        if(new_dividend == 0 ) return 0;
        if(new_dividend == new_divisor) return sign;
        if(new_divisor == 1) return new_dividend*sign;
        if(new_divisor>new_dividend ) return 0;
        
        return get_mul(new_dividend, new_divisor)*sign;
        
    }
    int get_mul(long long int dividend, long long int divisor){
        //record current lower sum
        long long int sum = divisor;
        //record current multiplier
        long long int tmp_mul = 1;
        long long int mul = 0;

        
        while(dividend > divisor){
            
            //exponential increase
            while(sum+sum <= dividend){
                if(sum == 0){
                    sum+= divisor;
                    tmp_mul = 1;
                }
                else{
                    sum += sum;
                    tmp_mul += tmp_mul;
                }
            }
            
            mul += tmp_mul;//increase multiplier
            dividend -= sum; //shift dividend to left since we shift temp sum to 0
            tmp_mul = 0;//shift temp multiplier to 0
            sum = 0;//shift temp sum to 0
        }
        
        
        return mul;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cpp leetcode