题目描述:

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

解题思路:

把除数表示为:dividend = 2^i * divisor + 2^(i-1) * divisor + ... + 2^0 * divisor。这样一来,我们所求的商就是各系数之和了,而每个系数都可以通过移位操作获得。

详细解说请参考:http://blog.csdn.net/whuwangyi/article/details/40995863

代码如下:

public class Solution {
public int divide(int dividend, int divisor) {
boolean flag = (dividend > 0 && divisor > 0)
|| (dividend < 0 && divisor < 0);
long absDividend = Math.abs((long) dividend);
long absDivisor = Math.abs((long) divisor);
long quotient = dividePositive(absDividend, absDivisor);
if (flag && quotient > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return flag ? (int) quotient : -(int) quotient;
}
public long dividePositive(long dividend, long divisor) {
if (dividend < divisor)
return 0;
long quotient = 1;
long originalDivisor = divisor;
while (dividend >= (divisor << 1)) {
quotient <<= 1;
divisor <<= 1;
}
return quotient + dividePositive(dividend - divisor, originalDivisor);
}
}

Java [leetcode 29]Divide Two Integers的更多相关文章

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

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  2. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  3. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  4. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  5. [LeetCode] 29. Divide Two Integers ☆☆

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

  6. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

  8. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  9. LeetCode: 29. Divide Two Integers (Medium)

    1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...

随机推荐

  1. web.xml常用元素

    web.xml文件是用来初始化配置信息:比如welcome页面.servlet.servlet-mapping.filter.listener.启动加载级别等.当你的web工程没用到这些时,你可以不用 ...

  2. MITK-Qt4.8.4(x64)+VS2012+Win7_X64 编译记录

    本文参考 http://blog.csdn.net/lanxuxml/article/details/9232529(中文) http://docs.mitk.org/nightly-qt4/Buil ...

  3. DevExpress GridControl 导出为Excel

    private void btnExport_ItemClick(object sender, EventArgs e)         {             SaveFileDialog sa ...

  4. 1038: [ZJOI2008]瞭望塔 - BZOJ

    Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...

  5. Maven系列--"maven-compiler-plugin"的使用、Maven之Surefire插件

    一."maven-compiler-plugin"的使用 http://my.oschina.net/poorzerg/blog/206856 二.Maven之Surefire插件 ...

  6. The 7th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Wu Xing

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3328 至今未看懂题意,未编译直接提交,然后 A了.莫名AC总感觉怪怪的. ...

  7. 配置JAVA的环境变量

    下面开始配置环境变量,右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME”,在“ ...

  8. strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签

    定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...

  9. POJ3690+位运算

    题意:给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. /* 64位的位运算!!! 题意: 给定一个01矩阵.T个询问,每次询问大矩阵中是否存在这个特定的小矩阵. (64位记录状 ...

  10. 使用SpringAop 验证方法参数是否合法

    (原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7683194) 1.依赖包    aspectjweaver.jar 其中Maven的配 ...