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. pyglet: a cross-platform windowing and multimedia

    pyglet pyglet: a cross-platform windowing and multimedia library for Python.

  2. 集成Dubbo服务(Spring)

    Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是 ...

  3. 执行testng appium用例失败,自动截图

    新建一个截图监听类ScreenShotListener ,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package com.fsssc.htsgl.ut ...

  4. PhoneGap 和 PhoneGap Build 是什么?

    PhoneGap是目前唯一支持7种平台的开源移动开发框架,支持的平台包括iOS.Android.BlackBerry OS.Palm WebOS.Windows Phone 7.Symbian和Bad ...

  5. python查看删除你微信的账号

    #应用环境:python2.7 #!/usr/bin/env python # coding=utf-8 from __future__ import print_function import os ...

  6. single-row function和muti-row function

    1.single-row function 指一行数据输入,返回一个值的函数. 常见的有 字符函数(如:substr) 日期函数(如:months_between) 数字函数(如:MOD) 转换函数( ...

  7. [jQuery] 使用jQuery printPage plugin打印其他頁面內容

    目標: 點選按鈕後可以打印其他頁面的內容,可用於套版.內部表單套印...等等. 程式碼: 1.View(HTML布局) <h2>維修申請單</h2> <form id=& ...

  8. [转]CSS目标伪类E:target

    CSS3 target 伪类不得不说那些事儿(纯CSS实现tab切换) 是不是觉得target有点眼熟?! 今天要讲的不是HTML的<a>标签里面有个target属性. target伪类是 ...

  9. JS基础函数

    函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块 <script> function demo(){ var a = 10; var b = 25; var sum = a + ...

  10. WebAPI中Message Handler与Filter的区别

    两者最主要的区别是关注点不同.Message Handlers应用到HTTP Request,Filters仅应用到controller/action上. 如果某种行为应用到大多数的Request时, ...