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. Time.deltaTime和Time.realtimeSinceStartup

    private float f = 0f;void Update () {      f += Time.deltaTime;      Debug.LogError ("Time.delt ...

  2. mma ctf 1st && csaw 2015

    (很久以前做的,现在发一下)最近做了两个CTF,水平太渣,做了没几道题,挑几个自己做的记录一下. mma ctf 1st 之 rps: from socket import * s = socket( ...

  3. RAID技术

    RAID:其基本思想就是把多个相对便宜的硬盘组合起来,成为一个硬盘阵列组,使性能达到甚至超过一个价格昂贵.容量巨大的硬盘.所以称为廉价磁盘冗余数组 RAID级别: RAID 0亦称为带区集.它是将多个 ...

  4. idea git 注意事项

    1.使用idea进行不同分支切换的时候,一定要先将代码当前分支的代码提交(可以不push),否则当前分支未提交的代码被认为即将切换的代码,在切换过程中会产生冲突. 2.使用idea进行merge的时候 ...

  5. javascript 【封装AJAX】

    post function createXHR() { if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); ...

  6. dependency injection via inversion of control

    依赖注入DI是一个程序设计模式和架构模型, 一些时候也称作控制反转,尽管在技术上来讲, 依赖注入是一个IOC的特殊实现, 依赖注入是指一个对象应用另外一个对象来提供一个特殊的能力, 例如:把一个数据库 ...

  7. (转)解决JSP路径问题的方法(jsp文件开头path, basePath作用)

    在JSP中的如果使用 "相对路径"  则有可能会出现问题. 因为 网页中的 "相对路径" , 他是相对于 "URL请求的地址" 去寻找资源. ...

  8. PHP简易计算器方法1

    <?phpheader("content-type:text/html;charset=utf-8");session_start();?><!DOCTYPE h ...

  9. docker 基于现有镜像修改后保存,上传私有仓库

    docker:/root# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3cb864be528 192.168.3 ...

  10. C语言的本质(1)——计算机与二进制

    人类的计数方式通常是"逢十进一",称为十进制(Decimal),大概因为人有十个手指,所以十进制是最自然的计数方式,各民族的文字中都有十个数字,而阿拉伯数字0-9是目前最广泛采用的 ...