class Solution(object):
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
flag=-1
if ( dividend > 0 and divisor >0 ) or ( dividend < 0 and divisor < 0 ):
flag=1
dividend=abs(dividend)
divisor=abs(divisor)
quotient=0
while dividend >= divisor:
k=0
tmp=divisor
while dividend >= tmp:
quotient += 1 << k
dividend -= tmp
tmp <<=1
k+=1 if flag > 0:
if quotient > 2147483647:
quotient = 2147483647
return quotient
else:
return -quotient

@link http://chaoren.is-programmer.com/posts/43017.html

leetcode Divide Two Integers python的更多相关文章

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

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

  2. LeetCode: Divide Two Integers 解题报告

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...

  3. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  4. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  5. Leetcode:Divide Two Integers分析和实现

    题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...

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

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

  7. leetcode面试准备:Divide Two Integers

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

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

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

  9. 【Leetcode】【Medium】Divide Two Integers

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

随机推荐

  1. Reverse Key Indexes反向索引

    Reverse Key Indexes反向索引A reverse key index is a type of B-tree index that physically reverses the by ...

  2. spring boot 中文文档

    https://qbgbook.gitbooks.io/spring-boot-reference-guide-zh/content/VII.%20Spring%20Boot%20CLI/index. ...

  3. UVA 1611 Crane

    题意: 输入一个1-n的排列,要求经过操作将其变换成一个生序序列.操作的规则如下每次操作时,可以选一个长度为偶数的连续区间,交换前一半和后一半. 分析: 假设操作到第i个位置,而i这个数刚好在pos这 ...

  4. 详解JOIN

    根据连接中使用的操作符不同,连接条件可分为:等连接,不等连接   连接本身分为: 内连接(INNER JOIN) (1)INNER JOIN 方式(INNER可以省略) 取两表的交集. (2)并表查询 ...

  5. "Classifying plankton with deep neural networks" notes

    cross entropy loss is not quite the same as optimizing classification accuracy. Althougth the two ar ...

  6. Picasso 加载图片到RelativeLayout之解决方案

    Picasso 加载图片到ImageView 或者自己的自定义View都是可以直接调用对应API的,但是用into(0直接也加载到RelatieLayout就不好使了,可以这样来: Picasso.w ...

  7. Android_打开多个Activity,返回到第一个Activity

    正文 一.流程截图 二.问题说明 依次从登录到三级界面,然后退出回到登录界面. 三.解决办法 3.1 实现代码 三级界面调用如下代码:         Intent intent = new Inte ...

  8. mysql性能优化学习笔记(5)数据库结构优化

    一.选择合适的数据类型    1.使用可存下数据的最小的数据类型    2.使用简单地数据类型,Int<varchar    3.尽可能使用not null定义字段    4.尽量少用text, ...

  9. JDK常见问题 环境变量配置

    "javac不是内部命令或外部命令" Windows7 安装"jdk-6u26-windows-x64.exe"后,常提示"javac不是内部命令或外 ...

  10. opencv如何用模板匹配寻找目标

    首先使用: MatchTemplate 比较模板和重叠的图像区域 void cvMatchTemplate( const CvArr* image, const CvArr* templ, CvArr ...