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

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

思路


  在题中明确了不能使用乘法和除法以及模运算,所以我们只能通过其他办法来进行解决。除法解决的问题就是看被除数里面有多少个除数的问题,最简单的方法就是我们可以使用减法,对被除数减去除数,然后计数加一。但是这种办法当从被除数较大时时间复杂度较高,因此我们可以对其进行改进,对被除数从除数的倍数开始减去,然后每次相减完毕之后对对除数进行加倍。这样当被除数比较大时,也能很快的相减完毕。

解决代码


 class Solution(object):
def divide(self, dividend, divisor):
positive = (dividend < 0) is (divisor < 0) # 被除数和除数为负数的情况进行考虑。
dividend, divisor = abs(dividend), abs(divisor) # 对其取绝对值
res = 0 # 结果
while dividend >= divisor: # 当被除数小于当前除数时,说明已经不能被整除了。
tem, i = divisor, 1 # 存储倍数和除数
while dividend >= tem: # 当被除数小于当前倍数除数时,终止循环
dividend -= tem # 被除数减去除数
res += i # 结果加上相应的倍数
i <<= 1 # 除数的倍数
tem <<= 1 # 除数翻倍
if not positive: # 判断是否有负数
res = -res
return min(max(-2147483648, res), 2147483647) # 超出范围判断

【LeetCode每天一题】Divide Two Integers(两整数相除)的更多相关文章

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

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

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

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

  3. [LeetCode] 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第28题--Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...

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

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

  7. [LintCode] Divide Two Integers 两数相除

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

  8. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  9. lintcode 中等题:Divide Two Integers 两个数的除法

    题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题  15%的通过率 ...

随机推荐

  1. ABP之事件总线(4)

    在上一篇的随笔中,我们已经初步完成了EventBus,但是EventBus中还有诸多的问题存在,那么到底有什么问题呢,接下来我们需要看一看ABP中的源码是如何定义EventBus的. 1.第一个点 在 ...

  2. 洛谷1443 马的遍历【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1443 题意: 给一个n*m的棋盘,马在上面走(规则就是象棋中的规则,详细见代码dx,dy数组定义) 问棋盘上 ...

  3. jdbc ---- DBUTilDao 类

    1, 列用工具包  阿里的 DbUtils: JDBC Utility Component Examples 再次封装成通用的 update, query package com.ljs.dao; i ...

  4. [No0000C8]英特尔快速存储IRST要不要装

    IRST (英特尔快速存储技术)Intel Rapid Storage Technology(简称IRST)又称英特尔快速存储技术. Intel Rapid Start Technolog同样是简称( ...

  5. linux虚拟机设置本地yum源

    1.挂载ISO镜像 2.创建文件夹,用于挂载光盘,mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom 3.修改 repo 文件 baseurl=file:///挂 ...

  6. Runtime Services

    Python Runtime Services — Python 3.7.2 documentation https://docs.python.org/3/library/python.html

  7. Server:www121 Server:www120 Server:NWS_SP

    Request URL:http://www.biyao.com/minisite/bzzx Request Method:GET Status Code:200 OK Remote Address: ...

  8. PHP进阶-浏览器到PHP发展历史

    从浏览器到PHP发展历史 php-cgi实现cgi的解析器,每个fork过程都开启一个进程,并会进行一个关闭进程的操作. 长注内存解释器(一个进程) fastcgi 多进程共享一个端口是一个问题,多进 ...

  9. Python中给List添加元素的4种方法

    https://blog.csdn.net/hanshanyeyu/article/details/78839266 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持 ...

  10. day5_递归调用

    #递归的意思,函数自己调用自己#递归最多递归999次#递归的效率没有循环高 实例1-递归调用: count = 0 def say(): global count count += 1 print(' ...