Divide Two Integers 解答
Question
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
Solution
dividend = divisor * quotient + remainder
而我们知道对于任何一个数可以表示为Σi * 2x 其中i为0或1。所以我们可以用加法实现乘法。
a = a + a 等同于 a = a * 2
因此我们可以通过对divisor乘以2,求出最大的x,然后继续求出第二大,第三大的x', x''..
注意到可能有溢出问题,解决方法是将要计算的所有数先转为long。
public class Solution {
public int divide(int dividend, int divisor) {
boolean negative = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
long a = Math.abs((long)dividend);
long b = Math.abs((long)divisor);
if (a < b) {
return 0;
}
long step, sum, result = 0;
while (a >= b) {
step = b;
sum = 1;
while (step + step <= a) {
step += step;
sum += sum;
}
a = a - step;
result += sum;
}
result = negative == true ? -result : result;
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
return Integer.MAX_VALUE;
}
return (int)result;
}
}
Divide Two Integers 解答的更多相关文章
- [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. ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- LeetCode29 Divide Two Integers
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
随机推荐
- 瑞柏匡丞谈中国移动app的国际进阶路
当今3.0互联时代,已然形成了一个移动化,互动化,全球化的完整体系.瑞柏匡丞也在常年与国内外客户的交流沟通中有了自己的些许见解. 国内的移动产业的发展已然非常迅速,但也正是因为各类企业的不断崛起,能够 ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- Quartz定时调度CronTrigger时间配置格式说明与实例
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- Android AdapterView View的复用机制 分析
对于ListView.GridView相信大家都不陌生,重写个BaseView,实现对于的几个方法,然后就完毕了我们的界面展示.而且在大部分情况下,我们载入特别多的Item也不会发生OOM,大家也都明 ...
- C#用注册表开机自动启动某某软件
代码如下: public static void chkAutoRun(bool isRun) { if (isRun)//开机自动启动 { try { RegistryKey runKey = Re ...
- 新闻滚动marquee标签
先上代码: <marquee behavior="" direction="up" onMouseOver="this.stop()" ...
- NFinal 控制器—URL
URL路由规则 规则:http://网址/模块名/控制器的相对路径的文件名/函数名.htm 例: http://localhost/App/IndexController/Index.htmhttp: ...
- Temporary ASP.NET Files 文件夹中保存的是什么内容?[转]
转自:http://www.cnblogs.com/suiqirui19872005/archive/2007/05/14/746320.html ASP.NET 页面请求的处理过程需要使用一些临时文 ...
- mysql查询分组归类函数-group_concat,通常与group_by一起使用
select a.`name`,group_concat(b.name SEPARATOR'.') as persons from `group` as a,`person` as b,`person ...
- GacLib使用方法(一)
GacLib使用方法 这是vczh大神的GacLib库新手入门,为自己做点笔记,详细的信息可以参考网页.下面简单说说怎么在自己的程序中使用GacLib库,本文只是前述网址中新手教程的一点体验,使用的环 ...