描述:不使用 * / % 完成除法操作。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. jQuery ajax传递特殊字符参数(例如+)

    使用jQuery ajax向后台传递参数para=1+1时后台接收到的参数为para=1 1,解决方案是 使用json传递,代码如下. var url = "/test/check" ...

  2. Android.mk 的含义

    LOCAL_PATH:=$(call my-dir) LOCAL_PATH是定义源文件在哪个目录用的. my-dir 是个定义的宏方法, $(call my-dir)就是调用这个叫 my-dir的宏方 ...

  3. Spring远端调用的实现-Spring Http调用的实现

    1:Spring Http设计思想 最近在研究公司自己的一套rpc远程调用框架,看到其内部实现的设计思想依赖于spring的远端调用的思想,所以闲来无事,就想学习下,并记录下. 作为spring远端调 ...

  4. An NIO.2 primer--reference

    Part 1: The asynchronous channel APIs The More New I/O APIs for the Java™ Platform (NIO.2) is one of ...

  5. 关于自己的ES6使用姿势

    ES6今年开始学的,从看文档到实践,以下是自己使用过的一些ES6的东西: 1:for-of 语法: 最喜欢的还是它支持了break/continue的语法,而且还修改了for-in的缺陷,简要写法: ...

  6. PHP in_array不兼容问题

    做过日本的手机端,就因为in_array这个方法在我的环境下没有问题 结果到日本那边就是出问题,一直纠结的我啊,现在特贴出当初的兼容方法 function in_into($key,$array){  ...

  7. 当前时间 js

    当前时间-倒计时下载 效果: 代码: <!doctype html> <html> <head> <meta http-equiv="Content ...

  8. MVC 中的Areas支持

    在ASP.NET MVC 2中对于Area功能的增强,这样的增强是如何在同一个项目中更好地组织应用程序的? ASP.NET MVC 1.0时,如果我们要在一个项目中做自己网站的后台应用,而又保持URL ...

  9. Microsoft SQL Server 管理 (常用管理及维护命令)

    --查询当前连接的实例名 select @@servername --察看任何数据库属性 sp_helpdb master --设置单用户模式,同时立即断开所有用户 alter database No ...

  10. .NET(C#):获取进程的CPU使用状况

    第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...