mycode   91.28%

class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
if divisor == 0:
return None
if((dividend^divisor)<0):
flag = -1
else:
flag = 1
dividend = abs(dividend) if dividend < 0 else dividend
divisor = abs(divisor) if divisor < 0 else divisor
MAX = 2147483647
MIN = -2147483648
res = (dividend // divisor)
if flag == -1:
return max(MIN,flag*res)
else:
return min(MAX,res)

参考:

思路:其实时不能用除法运算的,但是我还是用了。。。。

这道题的要求是在不使用乘法、除法、取模运算的前提下实现两个整数相除。如果溢出,返回MAX_INT。这道题的直接思路是用被除数不断减去除数,直到为0。这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n)。可以采用位运算进行优化,即模拟计算机上的除法运算。将整数转化成二进制形式,即num = a0*2^0 + a1*2^1 + a2*2^2 + ... + an*2^n。基于以上这个公式以及左移一位相当于乘以2,可以先让除数左移直到大于被除数之前得到一个最大的基数。然后每次用被除数去减去这个基数,同时结果增加2^k。接下来继续重新左移除数左移迭代,直到被除数不大于除数为止。因为这个方法的迭代次数是按2的幂直到结束,所以时间复杂度为O(logn)。值得注意的地方,主要就是处理符号和溢出问题。对于溢出问题,可以先采用long long进行计算,也可以在移位前判断移位后是否溢出。

#时间复杂度:O(logn)
#空间复杂度:O(1)

def divide(dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
positive = (dividend < 0) is (divisor < 0)
dividend, divisor = abs(dividend), abs(divisor)
res = 0
while dividend >= divisor:
temp, i = divisor, 1
print(dividend,divisor,temp,i,res)
while dividend >= temp:
dividend -= temp
res += i
i <<= 1
temp <<= 1
print('..',dividend,divisor,temp,i,res)
if not positive:
res = -res
return min(max(-2147483648, res), 2147483647)

下面这个更好理解些

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 左移

leetcode-mid-math-29. Divide Two Integers-NO的更多相关文章

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

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

  2. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  3. [LeetCode] 29. Divide Two Integers 两数相除

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

  4. Java [leetcode 29]Divide Two Integers

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

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

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

  6. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  7. [LeetCode] 29. Divide Two Integers ☆☆

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

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

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

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

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

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

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

随机推荐

  1. Elasticsearch入门教程(四):Elasticsearch文档CURD

    原文:Elasticsearch入门教程(四):Elasticsearch文档CURD 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...

  2. jsp-TagLib标签库

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...

  3. softmax+交叉熵

    1 softmax函数 softmax函数的定义为 $$softmax(x)=\frac{e^{x_i}}{\sum_j e^{x_j}} \tag{1}$$ softmax函数的特点有 函数值在[0 ...

  4. zabbix修改默认字体

    在zabbixweb界面里有时候显示界面为框框 所以修改默认字体 现在windows里  随便找个字体: win+R  搜fonts 然后随便复制了一个字体,传到zabbix服务器里 cp calib ...

  5. C++ STL(一)string的用法

    # 1.string的用法- string在<iostream>的头中就有,但是还是得用<string>的头 ##### 构造函数- str(const char* ch) 直 ...

  6. Binary Numbers AND Sum CodeForces - 1066E (前缀和)

    You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...

  7. Gym-100814K 数位DP 模拟除法

    Johnny is a brilliant mathematics student. He loves mathematics since he was a child, now he is work ...

  8. robotframework ride报错 Keyword 'BuiltIn.Log' expected 1 to 5 arguments, got 12.

    错误原因,else和else if使用了小写,必须使用大写才能识别到.

  9. Web Api 接口测试工具:Swagger

    前言:WebApi接口开发完毕后,交付给前端人员或手机端开发者时接口说明文档是必不可少的配套设备,如果公司流程不规范大家使用口口相传的交接方式,而且没有改进的欲望,那你可以到此为止了.Swagger是 ...

  10. linux下的命令和常见问题笔记

    nginx的三大功能: 1.http服务 2.反向代理 3.负载均衡 2.当nginx重启报:[root@localhost logs]# service nginx reloadReloading ...