#Leet Code# Divide Two Integers
描述:不使用 * / % 完成除法操作。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的更多相关文章
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- 算法练习--LeetCode--29. Divide Two Integers
Divide Two Integers Given two integers dividend and divisor, divide two integers without using multi ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
随机推荐
- 改变JVM中的参数以提高Eclipse的运行速度
首先建立评估体系,将workspace里所有的项目close掉,关闭eclipse.优化的用例就是启动eclipse,open一个项目,eclipse会自动build这个项目,保证没有感觉到明显的卡, ...
- [D3] 10. Creating Axes with D3
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 【JavaScript设计模式系列---开篇预览】
转:http://www.cnblogs.com/Darren_code/archive/2011/08/31/JavascripDesignPatterns.html 2011-08-31 23:5 ...
- careercup-数组和字符串1.8
1.8 假定有一个方法isSubstring,可检查一个单词是否为其他字符串的子串.给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次isSubstring.旋转字符串: ...
- 5 Common Interview Mistakes that Could Cost You Your Dream Job (and How to Avoid Them)--ref
There have been many articles on our site on software testing interviews. That is because, we, as IT ...
- Ubuntu开机启动svn
一.创建脚本 $cd /etc/init.d/$sudo vim svnserve 内容如下#! /bin/shsudo svnserve -d -r /home/svn 二.脚本权限设置 sudo ...
- android开发之Bundle使用
android开发中,我们经常需要在两个activity之间传递数据,最常用的莫过于使用intent.putXXX(),可是很多时候我们也会这样: Bundle bundle = new Bundle ...
- Android(java)学习笔记182:保存数据到SD卡 (附加:保存数据到内存)
1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...
- Android Studio导入aar依赖文件
以shareSDK为例,导入SMSSDK-2.1.1.aar: 首先将这个aar文件粘贴到libs文件夹下,然后在app目录下的build.gradle里操作 repositories{ flatDi ...
- sql执行万条update语句优化
几个月没有更新笔记了,最近遇到一个坑爹的问题,顺道记录一下.. 需求是这样的:一次性修改上万条数据库. 项目是用MVC+linq的. 本来想着用 直接where() 1 var latentCusto ...