Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

这道题属于数值处理的题目,对于整数处理的问题,比较重要的注意点在于符号和处理越界的问题。对于这道题目,因为不能用乘除法和取余运算,我们只能使用位运算和加减法。比较直接的方法是用被除数一直减去除数,直到为0。这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n)。 直接用除数去一个一个加,直到被除数被超过的话,会超时。

那么有没有办法优化呢? 这个我们就得使用位运算。我们知道任何一个整数可以表示成以2的幂为底的一组基的线性组合,即 num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大 于被除数之前得到一个最大的基。然后接下来我们每次尝试减去这个基,如果可以则结果增加加2^k,然后基继续右移迭代,直到基为0为止。因为这个方法的迭 代次数是按2的幂知道超过结果,所以时间复杂度为O(logn)。代码如下:

解决办法每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作。

class Solution {
public:
int divide(int dividend, int divisor) { if (dividend == INT_MIN && divisor == -)
return INT_MAX;
if (dividend == || divisor == )
return ; int nega = ;
if ((dividend>&&divisor<) || (dividend<&&divisor>))
nega = ;
long long d=dividend;//int数据abs(-2147483648)会溢出,因为正数int只能到2147483647,所以需要long long 来存储一下
long long s=divisor;
long long den = abs(d);
long long sor = abs(s);
if (sor > den)
return ;
long long sum = ;
int count = ;
int res = ;
while (den >= sor)
{
count = ; //a >= b保证了最少有一个count
sum = sor;
while (sum + sum <= den){ //!!
sum += sum;
count += count;
}
den -= sum;
res += count;
} if (nega)
res = - res;
return res;
}
};

这种数值处理的题目在面试中还是比较常见的,类似的题目有 Sqrt(x)Pow(x, n) 等。上述方法在其他整数处理的题目中也可以用到,大家尽量熟悉实现这些问题。

Divide Two Integers——二分法的经典变形的更多相关文章

  1. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  2. LeetCode第[29]题(Java):Divide Two Integers

    题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...

  3. Divide Two Integers(模拟计算机除法)

    Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...

  4. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  6. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  7. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  8. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

  9. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

随机推荐

  1. JavaScript定义类的方式与其它OO语言有些差异

    JavaScript面向对象的程序编写与其它OO语言有一些出入,所以使用JavaScript的面向对象特性的时候,需要注意一些规范性的问题.下面就简单地谈一下,JavaScript如何定义一个类,在定 ...

  2. django-jet 中文文档

    关于 JET是新式的Django管理界面并且增强了功能.     内容 文档 开始 安装django-jet 安装仪表盘 配置 配置文件 自动补全 紧凑内联 过滤器 仪表盘 自定义仪表盘 仪表盘模块 ...

  3. HDU4022 Bombing STL

    Bombing Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Su ...

  4. [mysql]数据库引擎查看

    1.查看数据库引擎 全局下,show engines; 2.察看数据库引擎 show variables like '%engine%'; 或者show create table xxx\G 会显示默 ...

  5. apt-get update的hit和ign含义

    How do Ign and Hit affect apt-get update? From what I can see in the apt source code, "Ign" ...

  6. DataXceiver error processing unknown operation src: /127.0.0.1:36479 dst: /127.0.0.1:50010处理

    异常信息如下: 2015-12-09 17:39:20,310 ERROR datanode.DataNode (DataXceiver.java:run(278)) - hadoop07:50010 ...

  7. [LeetCode] 10. Regular Expression Matching ☆☆☆☆☆

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. Stirling数笔记

    Updating.... 这几个玩意儿要记的东西太多太乱所以写blog整理一下 虽然蒯的成分会比较多全部 我居然开始记得写blog了?? 第一类 这里讨论的是无符号类型的. OEIS编号A130534 ...

  9. MyBatis框架的使用及源码分析(五) DefaultSqlSessionFactory和DefaultSqlSession

    我们回顾<MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置与使用> 一文的示例 private static SqlSessionFactory getSessionF ...

  10. 元类编程-- metaclass

    #类也是对象,type创建类的类 def create_class(name): if name == "user": class User: def __str__(self): ...