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. 【HDU 2160】母猪的故事

    题 Description 话说现在猪肉价格这么贵,著名的ACBoy 0068 也开始了养猪生活.说来也奇怪,他养的猪一出生第二天开始就能每天中午生一只小猪,而且生下来的竟然都是母猪. 不过光生小猪也 ...

  2. Excel 计算 tips

    1.  对一列数据想看看,distinct的结果 选中数据区域(包含列名),插入pivot table 2. 想检查一个单元格的值在不在某一列中,并返回标志值 =IF (COUNTIF(B:B,A1) ...

  3. codeforces 719A:Vitya in the Countryside

    Description Every summer Vitya comes to visit his grandmother in the countryside. This summer, he go ...

  4. dijkstra,SPFA,Floyd求最短路

    Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...

  5. codeforce 626E(二分)

    E. Simple Skewness time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  6. 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片

    - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...

  7. 锋利的jQuery-3--$()创建节点

    创建节点可以用jquery的工厂函数,$() $() 会根据传入的html标记字符串,创建一个dom对象,并将这个dom对象包装成一个jquery对象后返回. var li_1 = $("& ...

  8. Windows 2008远程多用户登录的配置方法(转载)

    在使用Windows2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→属性→远程配置→远程桌面,就可以配置相应的远程桌面功能了.下 ...

  9. C#中跨线程访问控件问题解决方案

    net 原则上禁止跨线程访问控件,因为这样可能造成错误的发生,推荐的解决方法是采用代理,用代理方法来间接操作不是同一线程创建的控件. 第二种方法是禁止编译器对跨线程访问作检查,可以实现访问,但是出不出 ...

  10. Spark-1.0.0 standalone分布式安装教程

    Spark目前支持多种分布式部署方式:一.Standalone Deploy Mode:二Amazon EC2.:三.Apache Mesos:四.Hadoop YARN.第一种方式是单独部署,不需要 ...