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 解答的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  3. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  4. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

  6. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  7. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  8. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  9. LeetCode29 Divide Two Integers

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

随机推荐

  1. CentOS7 定时检测进程占用内存大小,执行重启进程操作(xjl456852原创)

    在使用CentOS时,个别程序的进程会一直增大,直到宕机.但是这种程序本身有没有更好的版本使用(比如ngrok免费就这样,付费的就没这个问题),所以想写一个脚本定时检测一下这个程序的情况,决定是否需要 ...

  2. Html5学习笔记(一)

    一:常见标签类型 块级标签 特点:1.独占一行 2,可以随时设置w,h   2.行内标签(内联) 特点: 1.多个行内标签能同时显示在一行 2.w.h取决于内容的尺寸()   3.行内-块级标签 特点 ...

  3. jQuery 之$.proxy() 方法

    定义和用法 $.proxy 方法接受一个已有的函数,并返回一个带特定上下文的新的函数. 该方法通常用于向上下文指向不同对象的元素添加事件. 参数 描述 function 要被调用的已有的函数. con ...

  4. java实验7-多线程编程

    1 利用Thread和Runnable创建线程 [实验目的] (1)理解用实现Runnable接口的方法实现多线程. (2)掌握线程优先级的设置. (3)加深对线程状态转换的理解. [实验要求] 要求 ...

  5. AngularJS移动开发中的坑汇总

    使用AngualrJs开发移动App已经快半年了,逐渐积累了非常多AngularJS的问题,特别是对于用惯了Jquery的开发人员,转到AngularJS还是须要克服非常多问题的.不像Jquery那样 ...

  6. Socket学习笔记

    ..........(此处略去万万字)学习中曲折的过程不介绍了,直接说结果 我的学习方法,问自己三个问题,学习过程将围绕这三个问题进行 what:socket是什么 why:为什么要使用socket ...

  7. EF中执行原生sql与使用Local获取本地数据

    使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据) using (var context = new BloggingContext()) { / ...

  8. 重写OnPaint事件对窗体重绘 实例1

    public class WindowOne:Form { public WindowOne() { InitializeCompnent(); } public void InitializeCom ...

  9. unity针对iphone的屏幕旋转

    屏幕旋转可以在引擎里设置: 依次点开 Edit——Project Setting——Player 即可设置如图: 接下来的是 雨松大神的 代码控制,本屌是安卓机器,没能测试. C# using Uni ...

  10. 关于Jquery.validate.js中动态删除验证remove方法的Bug

    利用Jquery.validate.js 来做动态验证的时候,需要特定的情况下,删除添加opAmount的必须入力的Check $("#form").validate({ rule ...