转载: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要小
最后返回值的时候不要忘记注意正负号

  1. public int divide(int dividend, int divisor) {
  2. if (dividend == 0 || divisor == 0)
  3. return 0;
  4. boolean flag = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
  5. long a = Math.abs((long) dividend);// 注意要转换成long型,防止溢出
  6. long b = Math.abs((long) divisor);
  7. if (a < b)
  8. return 0;
  9. int result = 0;
  10. while (a >= b) {
  11. long tempSum = b;
  12. long divSum = 1;
  13. while (tempSum + tempSum <= a) {
  14. tempSum += tempSum;
  15. divSum += divSum;
  16. }
  17. a -= tempSum;
  18. result += divSum;
  19. }
  20. return flag == true ? -result : result;
  21. }

[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. P3501 [POI2010]ANT-Antisymmetry

    P3501 [POI2010]ANT-Antisymmetry 二分+hash 注意:答案超出int范围 ------------ 先拿一个反对称串来做栗子:010101 我们可以发现 0101(左边 ...

  2. 获取Json字符串中的key和value

    获取Json字符串中的key和value 在web项目中经常会用到json数据(如:struts2处理请求返回json数据给jsp解析),因此,JSONObject对象是必备的,这时就需要引入相关的j ...

  3. 20145335郝昊《网络攻防》Exp9 Web安全基础实践

    20145335郝昊<网络攻防>Exp9 Web安全基础实践 实验内容 理解常用网络攻击技术的基本原理. 完成WebGoat实践下相关实验 实验步骤 XSS注入攻击 Phishing wi ...

  4. 小Z的袜子(莫队分块)题解

    小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  5. 【附9】elasticsearch-curator + Linux定时任务

    官网教程入口:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html 一.下载安装 下载:sud ...

  6. HDU 5963 朋友(找规律博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5963 题意: 思路: 我们可以先只考虑单链,自己试几种案例就可以发现规律,只有与根相连的边为1时,只需要奇数次操 ...

  7. 51nod 1445 变色DNA(最短路变形)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445 题意: 思路: 挺好的一道题目,如果$colormap[i][j] ...

  8. hdu 5381 The sum of gcd 莫队+预处理

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  9. hdu 5525 Product 数论算贡献

    Product Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Proble ...

  10. DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值

      比如要求取出微信绑定的,没有解绑的 未绑定,指定字段为null 绑定的,指定字段为某个字符串 解绑的,有的客户用的是更新指定字段为1,有的客户更新指定字段为‘1’ 脏数据的存在,比如该字段为空字符 ...