Algorithms practice:leetcode 29. Divide Two Integers

Description

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.

Return the quotient after dividing dividend by divisor.

Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231.

Example

Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333… which is truncated to 3.

Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333… which is truncated to -2.

Constraints

-231 <= dividend, divisor <= 231 - 1
divisor != 0

code

class Solution {
public:
     int divide(int dividend, int divisor) {
        if(dividend == divisor)
            return 1;

        unsigned int a = abs(dividend);
        unsigned int b = abs(divisor);
        bool isPositive = (dividend<0 == divisor<0);
        unsigned int result=0;
        if(a<b)
            result=0;
        
        while(a>=b)
        {
            short index=0;
            while(a>(b<<(index+1)))
                index++;
            
            a=a-(b<<index);
            result = result+(1<<index);
         
        }
        
        if(result == (1<<31) and isPositive)   // if ans cannot be stored in signed int
            return INT_MAX;
        return isPositive?result:-result;
    }

};

Complete Thinking Process | Intuitive Explanation | All rules followed | C++ code

Oral process of solving problems

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. So we use bit manipulation. we can take an example. dividend equals 10 in binary which is expressed as 1010. and divisor equal 3. The binary form of 3 is 0011.

the division formula is Dividend = Divisor x Quotient + Remainder. The formula brings in value to get a result.
1010 = 0011 * Quotient +Remainder.
(0011 | 1010 )

we declare one variable for the result, which is named result.
Divide the first digit of the dividend by the divisor. 1 is the first digit of the dividend. but 1 is small than Divisor(0011). So 1 divided divisor is itself. In this case, write a 0 above the division bar, aligned above the 1. the current Quotient is 0, So the result is 0. the current Remainder is 1.

then we get the second digit of the dividend 0 and place it beside the last Remainder 1, giving you temp dividend 10. Temp dividend is small than Divisor. So the current Quotient is 0, So the result is 0. the current Remainder is 10.

we get the third digit of the dividend 1 and write it to the right of the last Remainder. we get temp dividend 101. the dividend is big than Divisor. In this case, we should minus the dividend by the divisor. 101 minus 11 is 10. so the current Quotient is 1, and the current Remainder is 10. So the result is 1.

we get the fourth digit of the dividend 0 and write it to the right of the last Remainder. so the temp dividend is 100, which is big than the divisor. 100 minus divisor 11 is 1. the current Quotient is 1, then place it beside the result. now the result is 11. the current Remainder is 1.

the final result is 11 in binary form.

let’s code up the solution.
Since we are not allowed to have any numerical data type bigger than a 32-bit integer, we will use the last bit reserved for a sign to avoid overflow by using the unsigned int in c++.

words

  1. integer [ˈɪntɪdʒə®] :
  2. divisor [dɪˈvaɪzə®] :(mathematics 数) a number by which another number is divided 除数;除子
  3. dividend [ˈdɪvɪdend]: mathematics : a number to be divided
  4. multiplication[ˌmʌltɪplɪˈkeɪʃn]:N-UNCOUNT 不可数名词乘法;乘法运算 Multiplication is the process of calculating the total of one number multiplied by another. (There will be simple tests in addition, subtraction, multiplication and division.会有对加、减、乘、除运算的简单测试。)
  5. division [dɪˈvɪʒn]
  6. mod [mɒd]
  7. truncate [trʌŋˈkeɪt]:to make sth shorter, especially by cutting off the top or end
  8. fractional [ˈfrækʃənl]:(formal) very small; not important
  9. quotient [ˈkwəʊʃnt](mathematics 数) a number which is the result when one number is divided by another 商(除法所得的结果)
    10.Assume [əˈsjuːm]

link

youtubu
How to Do Division

10001 is the binary way of writing the number 17.
110011 is the binary number that represents 51.
50 in binary is 110010.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值