码迷,mamicode.com
首页 > 其他好文 > 详细

29. Divide Two Integers

时间:2018-05-04 17:06:56      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:code   style   question   trunc   sign   divide   ++   class   bit   

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.

思考:我们先暂不考虑特殊的输入,一开始的方法是被除数不停的减去除数,直到余数小于除数。结果是超时。想了想,确实,如果被除数是个很大的,除数是个很小的数,那么时间确实很长。

怎么办呢?能不能减去更大的数,而不是除数本身。看看题目,又不能用乘法,除法,取模。想到底层向左移动一位,解释出的整数值就会变成原来的2倍。

就有了这样的解法:首先不停的对除数乘以2,乘以2,乘以2,直到最靠近被除数,然后减去这个数。如此往复。具体的时间复杂度我也不是很清楚,不过既然用到了指数增长,应该很快吧。

这样还不行,还有很多特殊用例,最小负数-2147483648,最大整数2147483647,特殊输入的个数总是只有几个,我进行的特殊的处理就行了。

 

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

 

 

29. Divide Two Integers

标签:code   style   question   trunc   sign   divide   ++   class   bit   

原文地址:https://www.cnblogs.com/midhillzhou/p/8990900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!