leetcode #29 in cpp

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;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值