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, ...
随机推荐
- hdu3308--LCIS 最大连续递增序列长度
这个是动态的,所以要用线段树维护.代码里有注释因为ls敲成lsum,rs敲成rsum查错查了好久.. #include <set> #include <map> #includ ...
- <转载>Win32控制台工程中创建窗口
有的时候,用控制台同步输出调试信息.程序状态量,比出Log.弹出报错对话框等方法来得有效.那么如何做到呢?如下: 简而言之,用GetModuleHandle()函数获得当前程序实例句柄,其它地方与常见 ...
- <php>上传文件的程序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 禁用menu键
发现很多应用中基本不再使用menu键来显示菜单了,而在android studio中创建一个activity时,默认使用了actionBar,就是在右上角会显示三个点的内容,点击会出现Settings ...
- bootstrap学习以及其插件
Bootstrap中文网地址,里面有bootstrap组件的下载与使用说明,现在使用bootstrap3: http://www.bootcss.com/ W3CSchool.CC里面有学习boots ...
- phoneGap开发环境搭建(android)
1. 首先安装nodejs (http://nodejs.org/) 2. 然后在命令行输入 npm 回车 假设出现下图: 则表示成功安装 3. 安装 npm install -g cordov ...
- OLE-DB 操作excel 基本
1 方法用例 *&---------------------------------------------------------------------* *& 本程序总结了常 ...
- traceroute原理
traceroute原理 ICMP ICMP全称为Internet Control Message Protocol,即,网络控制报文协议. 当一个IP数据报发送失败时,最后一个路由器会向发送发传递一 ...
- samba错误
1.session setup failed: NT_STATUS_LOGON_FAILURE 该错误表示用户有误, 可能是用户不存在, 也有可能是密码错误, 或者用户只是在samba和系统的用户中的 ...
- React数据传递
React基础概念 React是基于组件化的开发,通过组件的组合,让web应用能够实现桌面应用的效果. React更有利于单页应用的开发. 并非MVC框架,只能算是V 具有单项数据流的特点 优势:代码 ...