leetcode-mid-math-29. Divide Two Integers-NO
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的更多相关文章
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- jmeter强大的扩展插件!!
jmeter4.0以上版本,如jmeter5.1.1版本的集成插件,只需要在官网下下载“plugins-manager.jar”包,放在jmeter安装路径的lib/ext目录下即可使用. (但该ja ...
- sqlserver2008 必知必会技巧-- 快速索引对象
对象资源管理器里面 -- 数据库 -- 表目录 ,然后按 f7 弹出 对象资源管理详细信息 , 里面有搜索栏 , 可以 使用 % 进行模糊查询 例如我们查包含 student的表 %student% ...
- ALV打印不显示打印界面的问题
用OO的方式screen0 不画屏幕会产生这个问题,解决办法就是不用screen0 要自己画一个区域
- 60. Permutation Sequence (JAVA)
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- python cv2的视频检测:睁眼闭眼
如题,想实现一个简单的根据摄像头的某一帧检测睁眼闭眼的功能. 初步的想法是: 1. cv2调用计算机摄像头,读取某一帧的画面. 2. 将该画面作为 哈尔-人脸分类器的输入接口,根据分类器结果返回分类的 ...
- Centos 7.5 双网卡内外网同时访问路由设置
说明:服务器有两张网卡分别是eth0.eth1,eth0配置内网IP:192.168.1.1/24,eth1配置外网IP:10.1.1.1/24:要求192.168.0.0/16网段走网卡eth0,网 ...
- scp 远程文件复制命令
scp 远程文件复制工具 1.命令功能 scp用户在不同linux主机间复制文件,他采用ssh协议保障复制的安全性.scp复制是全量完整复制,效率不高,使用与第一次复制,增量复制建议rsync命令. ...
- Tensort之uff
# This sample uses a UFF MNIST model to create a TensorRT Inference Engine from random import randin ...
- SQL代码
SELECT SCHEMA_NAME(SCHEMA_ID)AS ID,name as Table_name FROM sys.tables;--查询表视图 查询表视图
- Spring AOP 使用注解定义切面(转载)
原文地址:http://www.jianshu.com/p/6f40dddd71a5 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.a ...