题目来源:

  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. 51nod 1239 欧拉函数之和(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...

  2. ubuntu 常用生产环境部署配置测试调优

    1,ubuntu monogdb 安装配置 2,ubuntu jdk1.7,tomcat7安装 3,ubuntu LAMP部署 4,mongodb 远程热备份及恢复 使用自带的mongodump和mo ...

  3. rdo(remote data objects) repo openstack icehouse

    problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...

  4. ICE

    一.Slice-to-C++映射 1.引言 其映射定义:怎样把Slice数据类型翻译成C++类型,客户怎样调用操作.传递参数.处理错误. C++映射完全是线程安全的.例如,类的引用机制针对并行访问机制 ...

  5. Cookie获取、设置值

    设置: HttpCookie cookie = new HttpCookie("cookieName"); cookie.Value = "name1" Htt ...

  6. UVA1471( LIS变形)

    这是LIS的变形,题意是求一个序列中去掉某个连续的序列后,能得到的最长连续递增序列的长度. 用DP的解法是:吧这个序列用数组a来记录,再分别用两个数组f记录以i结尾的最长连续递增序列的长度,g[i]记 ...

  7. Windows配置Python编程环境

    1.安装Python https://www.python.org/ 2.修改环境变量 将安装python的路径加到path路径 3.配置notepad++ a. notepad++/运行/“运行”按 ...

  8. BZOJ 1316: 树上的询问( 点分治 + 平衡树 )

    直接点分治, 用平衡树(set就行了...)维护. -------------------------------------------------------------------------- ...

  9. textwrap——文本包裹和填充模块解析

    textwrap模块提供了两个函数wrap()和fill(),以及TextWrapper类,以及另外一个工具函数dedent().         wrap()以及fill()都可以用来格式化一大段文 ...

  10. ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)

    1.Widget类的使用方法: 第一步:在Action同级目录中新建Widget文件夹(独立分组需要自己建立) 第二步:根据不同功能在Widget文件夹中建立不同的Widget类,如热门文章HotWi ...