Divide Two Integers——二分法的经典变形
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——二分法的经典变形的更多相关文章
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- Divide Two Integers(模拟计算机除法)
Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
随机推荐
- Tomcat启动时报org.springframework.web.context.ContextLoaderListener错误解决方案
问题现象:新从svn上检出maven的项目maven+spring+springmvc项目在Tomcat启动时,报如下错误. 严重: Error configuring application lis ...
- HDU5533(水不水?)
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- rm删除破折号 - 开头的文件
解决这个问题的一个方法就是在要删除的文件的前边加上"./" # rm ./-slow_query_130103.txt.gz To remove a file whose name ...
- bzoj 2086 [Poi2010]Blocks 单调栈
[Poi2010]Blocks Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 788 Solved: 356[Submit][Status][Dis ...
- bzoj 1132 [POI2008]Tro 几何
[POI2008]Tro Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1796 Solved: 604[Submit][Status][Discu ...
- 南阳ACM 题目22:素数求和问题
素数求和问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和. ...
- zoj 2006 Glass Beads
Glass Beadshttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1006 Time Limit: 2 Seconds ...
- Spark的Shuffle过程介绍
Spark的Shuffle过程介绍 Shuffle Writer Spark丰富了任务类型,有些任务之间数据流转不需要通过Shuffle,但是有些任务之间还是需要通过Shuffle来传递数据,比如wi ...
- 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集
题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...
- Spring Boot 中使用 MyBatis 整合 Druid 多数据源
2017 年 10 月 20 日 Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...