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. ...
随机推荐
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...
- YBT 5.3 数位动态规划
记忆化搜索的专题 题解在代码中 Amount of Degrees[loj 10163] /* 此题可以转换成将10进制转成b进制后有k个1其他都为0的个数 所以用记忆化dfs dp[pos][sum ...
- [zhuan]动态链接库中的.symtab和.dynsym
http://blog.csdn.net/beyond702/article/details/50979340 原文如下: shared library (.so) "Program Lib ...
- Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机
Nginx的配置文件简介及在Nginx中配置基于不同ip的虚拟主机: #user nobody; worker_processes 1; #error_log logs/error.log; #err ...
- Codeforces Round #398 (Div. 2) A B C D 模拟 细节 dfs 贪心
A. Snacktower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Jokewithpermutation (DFS)
Problem J. Jokewithpermutation Input file: joke.inOutput file: joke.out Joey had saved a permutation ...
- HDU 1715 大数java
大菲波数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Getting Private/Public IP address of EC2 instance using AWS-cli [closed]
For private IP address: aws ec2 describe-instances --instance-ids i-b78a096f | grep PrivateIpAddress ...
- Difference between Netbios and Host name
Hostnames or NetBIOS names were used to provide a friendlier means of identifying servers or worksta ...
- asp:DropDownList与asp:DataList的联合使用
情况:当在asp:DropDownLis点击选取其中的一个值来响应datalist的值. <form id="form1" runat="server"& ...