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, ...
随机推荐
- Git本地版本控制备忘
首先git是一个版本控制工具,类似于SVN 笔记包括两部分,git本地版本控制和git远程协助 一.Git本地版本控制 以git windows版本msysgit为例,下载地址http://msysg ...
- paip.sql索引优化----join 代替子查询法
paip.sql索引优化----join 代替子查询法 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.n ...
- PHP常见报错解析
{错误类型}: {错误原因} in {错误文件} on {错误行数} 说明了在哪个文件的哪一行中因何种原因出现了何种错误. 常见的错误类型一般有下面几种: Parse error(解析错误)一般都伴随 ...
- <PHP>字符串处理代码
字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串 ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 解决办法
Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (d ...
- SecureCRT 安装及初始化配置
安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...
- (转)Building MariaDB on Mac OS X using Homebrew
https://kb.askmonty.org/en/building-mariadb-on-mac-os-x-using-homebrew/ Work has been done to provid ...
- resin安装和配置
1 从 http://www.caucho.com/download/ 下载resin 2 检查你的linux环境,查看是否安装了 jdk1.5 或以上版本,是否安装了perl. 输入命令:Java ...
- jQuery之.html()和.text()区别
.html()//获取标签和内容 .text()//只获取内容
- JavaScript基础学习
什么是变量! 什么是变量?从字面上看,变量是可变的量;从编程角度讲,变量是用于储存某种/某些数值的存储器.我们可以把变量看做一个盒子, 为了区分盒子,可以用BOX1,BOX2等名称代表不同盒子,BOX ...