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

If it is overflow, return MAX_INT.

解题思路:

既然不呢个用乘除和取模运算,只好采用移位运算,可以通过设置一个length代表divisor向左的做大移位数,直到大于dividend,然后对length做一个循环递减,dividend如果大于divisor即进行减法运算,同时result加上对应的值,注意边界条件,JAVA实现如下:

static public int divide(int dividend, int divisor) {
if (divisor == 0)
return Integer.MAX_VALUE;
boolean isNeg = dividend < 0;
if (divisor < 0)
isNeg = !isNeg;
int length = 0;
long longdividend = Math.abs((long) dividend);
long longdivisor = Math.abs((long) divisor), result = 0;
while (longdividend >= longdivisor) {
longdivisor = longdivisor << 1;
length++;
}
while (length >= 0) {
if (longdividend >= longdivisor) {
result += Math.pow(2, length);
longdividend -= longdivisor;
}
longdivisor = longdivisor >> 1;
length--;
}
if (result > Integer.MAX_VALUE && !isNeg)
result = (long) Integer.MAX_VALUE;
return isNeg ? -(int) result : (int) result;
}

Java for LeetCode 029 Divide Two Integers的更多相关文章

  1. LeetCode 029 Divide Two Integers

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

  2. Java [leetcode 29]Divide Two Integers

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

  3. 【LeetCode】029. Divide Two Integers

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

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

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

  5. 【leetcode】Divide Two Integers (middle)☆

    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, divisio ...

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

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

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

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

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

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

随机推荐

  1. 淘宝中的UV,PV,IPV

    1.  UV & PV UV: 店铺各页面的访问人数,一个用户在一天内多次访问店铺被记为一个访客(去重) ; Unique visitors PV: 店铺内所有页面的浏览总量(次数累加); p ...

  2. Recon-Erlang线上系统诊断工具

    Erlang系统素以稳定可靠闻名,但是它也是c实现的,也是要管理比如内存,锁等等复杂的事情,也会出现Crash,而且crash的时候大部分原因是因为内存问题.为此erlang运行期提供了强大的自省机制 ...

  3. Xcode7中新技术

    Xcode7真加了两个重要的debug功能:1:Address Sanitizer: 再也不用担心 EXC_BAD_ACCESS 在项目的Scheme中Diagnostics下,选中enable ad ...

  4. Chrome浏览器插件

    Chrome 布局 1. 修改Chrome Dock side Chrome 更多工具 -> 开发者工具 -> Customsize and Control Dev Tools

  5. java的static块执行时机

    一.误区:简单认为JAVA静态代码块在类被加载时就会自动执行.证错如下: class MyClass1 { static {//静态块 System.out.println("static  ...

  6. jquery------添加jQuery对象方法

    my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...

  7. DedeCMS V5.7 Dialog目录下配置文件XSS漏洞

    漏洞地址及证明:/include/dialog/config.php?adminDirHand="/></script><script>alert(1);< ...

  8. mouseover和mouseout闪烁问题

    在父级元素上注册了mouseover和mouseout事件后,当鼠标移动到子元素上时,会触发父级的mouseout和mouseover事件,反复触发,形成闪烁. 原因: 一种是由于冒泡,子级的mous ...

  9. Visual Studio Online Integrations-Customer support

    原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  10. jQuery插件 -- Form表单插件jquery.form.js

    http://blog.csdn.net/zzq58157383/article/details/7718956 http://my.oschina.net/i33/blog/77250