转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180

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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3
Example 2:

Input: dividend = 7, divisor = -3
Output: -2

乘除取模都不让用。。那只有加减

思路解析:

判空,返回0
使用long类型的变量存储division和divisor的绝对值
如果除数小于被除数,返回0
使用加法完成除法,注意保存加了多少倍,使用嵌套循环,外层控制division大于divisor,里层控制倍数增加一倍是否比division要小
最后返回值的时候不要忘记注意正负号

public int divide(int dividend, int divisor) {
if (dividend == 0 || divisor == 0)
return 0;
boolean flag = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
long a = Math.abs((long) dividend);// 注意要转换成long型,防止溢出
long b = Math.abs((long) divisor);
if (a < b)
return 0;
int result = 0;
while (a >= b) {
long tempSum = b;
long divSum = 1;
while (tempSum + tempSum <= a) {
tempSum += tempSum;
divSum += divSum;
}
a -= tempSum;
result += divSum;
}
return flag == true ? -result : result;
}

[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 ☆☆

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

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

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

  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两数相除

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

  6. Java [leetcode 29]Divide Two Integers

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

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

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

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

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

  9. [leetcode] 29. divide two integers

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

随机推荐

  1. 20145206邹京儒MSF基础应用

    20145206邹京儒MSF基础应用 一.MS08_067漏洞渗透攻击实践 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版). 2.在VMware中设置两台 ...

  2. cogs 362. [CEOI2004]锯木厂选址

    ★★★   输入文件:two.in   输出文件:two.out   简单对比 时间限制:0.1 s   内存限制:32 MB 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来. ...

  3. openwrt如何单独编译uboot

    答:make package/boot/uboot-<chip series>/compile

  4. jquery hover最后解决 - 不再疑惑 - 例子在这里

    hover具有动画累计的bug, 可以使用 stop 或 filter(:not(:animated))来消除, 但是, 即使这样, 当鼠标反复滑入或滑出的时候, 虽然没有动画累计的问题, 但是 下面 ...

  5. ActiveMQ 集群配置 高可用

    自从activemq5.9.0开始,activemq的集群实现方式取消了传统的Pure Master Slave方式,增加了基于zookeeper+leveldb的实现方式,其他两种方式:目录共享和数 ...

  6. 抽象类的继承,接口的实现,接口类型数组的使用,根据instanceof判断(返回)是否该是哪一个类型,类型的强转.

    总觉得之前第2处有点问题,果然. 还需要instanceof判定一下,然后还需要把数组Animal[]转为Pet的才有方法play()~~~!

  7. json库的编译方法和vs2010中导入第三方库的方法

    json库的编译方法和vs2010中导入第三方库的方法 一.去相应官网下载json.cpp文件 Jsoncpp下载:https://sourceforge.net/projects/jsoncpp/  ...

  8. Python Sip [RuntimeError: the sip module implements API v11.0 to v11.2 but the PyQt5.QtCore module requires API v11.3]

    不知道原因,尝试卸载.编译安装均失败.只有这样曲线救国 import matplotlib matplotlib.use("WXAgg",warn=True) import mat ...

  9. JQUERY链式操作实例分析

    本文实例讲述了jQuery链式操作.分享给大家供大家参考,具体如下: 从过去的实例中,我们知道jQuery语句可以链接在一起,这不仅可以缩短代码长度,而且很多时候可以实现特殊的效果. <scri ...

  10. Oncomine: 一个肿瘤相关基因研究的数据库--转载

    如果你获得了一个肿瘤差异表达基因,想研究其是否可作为某种肿瘤的潜在标志物和靶点,又怕做实验会得到阴性结果,浪费时间和金钱,这时候你就应该想到Oncomine数据库了(www.oncomine.org) ...