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. M - Sum

    XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are placed in a line. T ...

  2. Qt 4.8.6 PCL 1.8.0 VS 2010 联合编译常见错误

    在Qt和PCL联合编译的过程中,会出现各种各样的错误,解决这些错误的过程真是痛苦万分,所以总结一些常见错误方便自己也方便他人.比如我们要编译PCL1.8.0中的apps中的point_cloud_ed ...

  3. EF Core 2.1变化

    EF Core 2.1随.NET Core 2.1一起发布,本篇文章总结一下EF Core的新增功能,先从简单的开始说. 一.延迟加载 延迟加载不用介绍了吧,直接看一下怎样配置吧.EF Core 2. ...

  4. 【转】asp.net项目在IE11下出现“__doPostBack”未定义的解决办法

    最近我们运营的网站有用户反馈在 IE 11 下<asp:LinkButton> 点击出现 "__doPostBack"未定义",经过一番google,终于知道 ...

  5. TOP100summit2017:豆瓣耿新跃---站在公司整体目标下看技术管理

    壹佰案例:耿新跃老师您好,很荣幸又一次邀请到您担任壹佰案例大会的联席主席,在去年的壹佰案例大会上,您给我们带来很多非常经典的案例点评和提炼.您在去年壹佰案例峰会上最大的感触是什么呢? 耿新跃:我个人最 ...

  6. EF将IEnumerable<T>类型转换为Dictionary<T,T>类型

    x 无标题 #region 博客Code {DBEntities}生成EFModel的时候自己命名的 using ({DBEntities} db = new { DBEntities }()) { ...

  7. ASP.NET异步

    1.ASP.NET线程模型 在WEB程序中,天生就是多线程的,我们知道,一个WEB服务可以同时服务器多个用户,我们可以想象一下,WEB程序应该运行于多线程环境中,对于运行WEB程序的线程,我们可以称之 ...

  8. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  9. 开启spark日志聚集功能

    spark监控应用方式: 1)在运行过程中可以通过web Ui:4040端口进行监控 2)任务运行完成想要监控spark,需要启动日志聚集功能 开启日志聚集功能方法: 编辑conf/spark-env ...

  10. Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染3配置webpack支持ssr

    安装 cross-env yarn add -D cross-env 安装 html-webpack-plugin yarn add -D html-webpack-plugin 安装 webpack ...