题目来源:

  https://leetcode.com/problems/divide-two-integers/


题意分析:

  不用乘法,除法和mod运算来实现一个除法。如果数值超过了int类型那么返回int的最大值。


题目思路:

  初步来说,有两个做法。

  ①模拟除法的过程,从高位开始除,不够先右挪一位。这种方法首先要将每一位的数字都先拿出来,由于最大int类型,所以输入的长度不超过12位。接下来就是模拟除法的过程。

  ②利用左移操作来实现出发过程。将一个数左移等于将一个数×2,取一个tmp = divisor,所以将除数tmp不断左移,直到其大于被除数dividend,然后得到dividend - tmp,重复这个过程。


代码(python):

 class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
ispositive = True
if dividend > 0 and divisor < 0:
ispositive = False
if dividend < 0 and divisor > 0:
ispositive = False
dividend = abs(dividend);divisor = abs(divisor)
if dividend < divisor:
return 0
num = [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
i = 9
newdividend = []
while i >= 0:
tmp = 0
while dividend >= num[i]:
tmp += 1;dividend -= num[i]
newdividend.append(tmp); i -= 1
tmpm = 0; ans = 0 ;i = 0
while i < 10:
while tmpm < divisor:
if i > 9:
break
j = 0; t = 0
while j < 10 and tmpm != 0:
t += tmpm; j += 1
tmpm = t + newdividend[i]; i += 1
if tmpm < divisor:
j = 0; t = 0
while j < 10 and ans != 0:
t += ans; j += 1
ans = t
if tmpm >= divisor:
k = 0
while tmpm >= divisor:
tmpm -= divisor; k += 1
j = 0; t = 0
while j < 10 and ans != 0:
t += ans; j += 1
ans = t + k
if ispositive:
if ans > 2147483647:
return 2147483647
return ans
if ans >= 2147483648:
return -2147483648
return 0 - ans

模拟过程

 class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
ispositive = True
if dividend > 0 and divisor < 0:
ispositive = False
if dividend < 0 and divisor > 0:
ispositive = False
dividend = abs(dividend);divisor = abs(divisor)
if dividend < divisor:
return 0
tmp = divisor
ans = 1
while dividend >= tmp:
tmp <<= 1
if tmp > dividend:
break
ans <<= 1
tmp >>= 1
nans = ans + self.divide(dividend - tmp,divisor)
if ispositive:
if ans > 2147483647:
return 2147483647
return nans
if ans >= 2147483648:
return -2147483648
return 0 - nans

左移


转载请注明出处:http://www.cnblogs.com/chruny/p/4893254.html

[LeetCode]题解(python):029-Divide Two Integers的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  2. 【LeetCode】029. Divide Two Integers

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

  3. Java for LeetCode 029 Divide Two Integers

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

  4. LeetCode 029 Divide Two Integers

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

  5. leetcode第28题--Divide Two Integers

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

  6. LeetCode(29)Divide Two Integers

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

  7. 029 Divide Two Integers 两数相除

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

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

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

  9. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  10. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

随机推荐

  1. eclipse 插件安装

    203.208.46.146 www.google.com203.208.46.146 dl.google.com203.208.46.146 dl-ssl.google.com

  2. SPOJ 687 Repeats(后缀数组+ST表)

    [题目链接] http://www.spoj.com/problems/REPEATS/en/ [题目大意] 求重复次数最多的连续重复子串的长度. [题解] 考虑错位匹配,设重复部分长度为l,记s[i ...

  3. gcc 的include path和lib path调整

    `gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v   ------------------------------ ...

  4. SpringMVC深入理解

    核心类与接口 - DispatcherServlet 前置控制器- HandlerMapping 请求映射(到Controller)- HandlerAdapter 请求映射(到Controller类 ...

  5. android UI进阶之弹窗的使用(2)--实现通讯录的弹窗效果

    相信大家都体验过android通讯录中的弹窗效果.如图所示: android中提供了QuickContactBadge来实现这一效果.这里简单演示下. 首先创建布局文件: <?xml versi ...

  6. 为什么C#动态调用Java的cxf多了bool型参数

    最近的一个项目需要C#调用Java的cxf发布的接口,接口参数文档只给我的是两个long型,但是通过我动态加载发现,参数是四个. 比如接口文档给的接口是 TestFunc(long, long); 而 ...

  7. 提高PHP编程效率

    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id']的速度是$row[id]的7倍. 3.echo比print快,并且使用echo的多重 ...

  8. JBoss 系列六十九:CDI 基本概念

    概述 如果说EJB,JPA是之前JEE(JEE5及JEE5之前)中里程碑式的规范,那么在JEE6,JEE7中CDI可以与之媲美,CDI(Contexts and Dependency Injectio ...

  9. 5.6.3 String类型

    String类型是字符串的对象包装类型,可以像下面这样使用String构造函数来创建. var stringObject = new String("hello world"); ...

  10. Linux系统之UpStart

    子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...