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. 【线段树求逆序数】【HDU1394】Minimum Inversion Number

    题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...

  2. 【最小费用最大流】【HDU1533】【Going Home】

    题意 给你一个类似这样的图 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 问所有H移动到所有m上花费最少的步数 以所有H ...

  3. 02Android用户界面优化之(一)Android Fragment

    一.使用Fragment 1.AndroidManifest.xml文件 <?xml version="1.0" encoding="utf-8"?> ...

  4. sqlserver高版本到低版本迁移

    从SqlServer2014 备份bak文件,在SqlServer2012中还原时遇到版本不兼容的问题. 经过leader 指导,可尝试以下办法处理数据迁移.共分三个步骤: 一.对需要迁移数据库生成s ...

  5. 使用VIM + Ctags

    通常在Linux或其他*Nix环境我们都使用VIM作为代码编辑工具,在纯命令终端下,它几乎是无可替代的. 它具有非常强大的扩展机制,在文字编辑方面基本上无所不能. 不过Emacs用户请不要激动,笔者还 ...

  6. phpcms 一些bug

    首页禁止使用{php $a = }; 因为phpcms在模板解析中默认用到了$a变量,如再次使用会导致phpcms的get标签return报错

  7. Constructor Prototype Pattern 原型模式(PHP示例)

    当一个类大部分都是相同的只有部分是不同的时候,如果需要大量这个类的对象,每次都重复实例化那些相同的部分是开销很大的,而如果clone之前建立对象的那些相同的部分,就可以节约开销. 针对php的一种实现 ...

  8. Git客户端使用

    1.下载安装包 git:  https://git-for-windows.github.io/index.html tortoisegit: https://download.tortoisegit ...

  9. rpm安装软件(需管理员权限)

    常用命名规范 linux-1.2.0-30.e16.i686.rpm rpm基本命令 安装rpm -i software.rpm 卸载rpm -e software 升级rpm -U software ...

  10. Painting Storages(ZOJ)

    There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to pai ...