29. Divide Two Integers

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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

Thinking: Let's ignore special input for now. The initial method is to continuously subtract the divisor from the dividend until the remainder is less than the divisor. The result is a timeout. After thinking about it, indeed, if the dividend is a large number and the divisor is a small number, then the time is indeed very long.

How to do it? Can you subtract a larger number than the divisor itself. Look at the title, but you can't use multiplication, division, or modulo. Thinking that the bottom layer is shifted one bit to the left, the interpreted integer value will be doubled.

There is such a solution: first keep multiplying the divisor by 2, multiply by 2, multiply by 2, until it is closest to the dividend, and then subtract this number. so back and forth. I don't know the specific time complexity, but since exponential growth is used, it should be very fast.

This is not enough, there are many special use cases, the minimum negative number is -2147483648, the maximum integer is 2147483647, the number of special inputs is always only a few, and the special processing I perform will do.

 

 1 class Solution {
 2 public:
 3     int divide(int dividend, int divisor) {
 4         //for special divisor==-2147483648.
 5         if(divisor==-2147483648) {
 6             if(dividend==-2147483648)
 7                 return 1;
 8             else
 9                return 0;
10         }
11         
12         
13         int count = 0;
14         //for special dividend==-2147483648.
15         if(dividend==-2147483648) {
16             if(divisor==1)
17                 return -2147483648;
18             if(divisor==-1)
19                 return 2147483647;
20             if(divisor<0) {
21                 dividend = dividend - divisor;
22                 count++;
23             }else {
24                 dividend = dividend + divisor;
25                 count++;
26             }
27         }
28         //for most case
29         int negative;
30         if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) 
31             negative = 1;
32         else
33             negative = 0;
34         
35         
36         dividend = dividend > 0 ? dividend : -dividend;
37      
38         divisor = divisor > 0 ? divisor : -divisor;
39         
40         int step = divisor;
41         int len = 1;
42        
43         while(dividend>=divisor) {
44             while((step < 2147483647/2) && ((step<<1)<dividend)) {
45                 step = step <<1;
46                 len = len<<1;
47             }
48             dividend = dividend - step;
49             count = count + len;
50             
51             len = 1;
52             step = divisor;   
53         }
54         
55         count = negative? -count : count;
56         return count;
57     }
58 };

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325259821&siteId=291194637