描述:不使用 * / % 完成除法操作。O(n)复杂度会超时,需要O(lg(n))复杂度。

代码:

 class Solution:
# @return an integer
def dividePositive(self, dividend, divisor):
if dividend < divisor:
return 0 sum = divisor
count = 1
while sum + sum < dividend:
sum += sum
count += count count += self.dividePositive(dividend - sum, divisor) return count def divide(self, dividend, divisor):
if dividend >= 0:
if divisor > 0:
return self.dividePositive(dividend, divisor)
else:
return 0 - self.dividePositive(dividend, 0 - divisor)
else:
if divisor > 0:
return 0 - self.dividePositive(0 - dividend, divisor)
else:
return self.dividePositive(0 - dividend, 0 - divisor)

#Leet Code# Divide Two Integers的更多相关文章

  1. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

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

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

  3. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  4. 算法练习--LeetCode--29. Divide Two Integers

    Divide Two Integers Given two integers dividend and divisor, divide two integers without using multi ...

  5. [LeetCode] Divide Two Integers 两数相除

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

  6. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  7. leetcode-【中等题】Divide Two Integers

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

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

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

  9. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

随机推荐

  1. [学习笔记]设计模式之Abstract Factory

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 在上篇笔记Builder设计模式中,时の魔导士祭出了自己的WorldCreator.尽管它因此能创造出一个有山有树有房子的世界,但是白 ...

  2. PAT 1080. Graduate Admission (30)

    It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...

  3. 利用Ajax把前端的数据封装成JSON格式发送到服务器端并写成XML格式在服务器的硬盘上

    1.首先要在前端把要发送的东西(这里是一个实例化的car对象)都准备好,利用Ajax发送到服务器端,代码如下: <html xmlns="http://www.w3.org/1999/ ...

  4. Delphi通过ICMP检测与远程主机连接

    { ping IP 地址(返回false or true) 2015-03-23} function PingHost(HostIP: String): Boolean; type PIPOption ...

  5. [D3] 2. Basics of SVG

    1. svg should use 'fill' prop instead 'background-color'2. svg width & height no need 'px'3. att ...

  6. Java GC 概念摘要

    很长时间,我想Java的GC做一个小小的总结,他有没有时间.根据最近看了java paper向上java gc文章,我觉得好,读读.顺便说一下,总结下. java paper的GC文章地址,里面有非常 ...

  7. python 面向对象深入理解

    面向过程 函数式编程 面向对象编程:面向对象是由类和对象组成,只要用类和对象实现的,就是面向对象编程 def  Bar():     print  "This is  Bar " ...

  8. NET/ASP.NET Routing路由(深入解析路由系统架构原理)(转载)

    NET/ASP.NET Routing路由(深入解析路由系统架构原理) 阅读目录: 1.开篇介绍 2.ASP.NET Routing 路由对象模型的位置 3.ASP.NET Routing 路由对象模 ...

  9. 关于 复制文本 然后Ctrl+V 操作的这个功能 貌似jq也没有封装。。。

    /* * copy */ function copy(){ var maintext=$("#myhtml").val(); alert(maintext); copyToClip ...

  10. 类似与三元表达式的 json 读取值

    需要先在项目中添加 json的dll json 序列里面的key在item.feeType里面必须存在 否则会报 未将对象引用到实例 myDr["feeType"] = Newto ...