leetcode Divide Two Integers python
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的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- Leetcode:Divide Two Integers分析和实现
题目要求我们用一个32位整数整除另外一个整数,但是不允许我们使用除法,乘法和取模运算. 有趣的问题,下面说一下我的思路: 首先,先给出两个正整数除法运算的过程.假设a为被除数,而b为除数.在计算机中无 ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- springmvc 传递和接收数组参数
java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...
- 性能强悍的开源关系数据库PostgreSQL
性能强悍的开源关系数据库PostgreSQL
- 常用WebService收集
尊重原著作:本文转载自http://www.cnblogs.com/tianguook/archive/2010/09/29/1838469.html 天气预报Web服务,数据来源于中国气象局Endp ...
- myql 注意事项
在[mysqld]下加入一行:lower_case_table_names=1,1为不区分大小写,0是区分大小写...并/etc/init.d/mysql restart即可...
- c#创建带参数的线程
1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...
- Python面向对象关系
首先了解一下Python面向对象中类型-实例和父类-子类的关系.下面的一些规则很有用. 当我们介绍许多不同的对象时,我们只用了两种关系(图4.1 关系): 是一类(is a kind of)(实线): ...
- mysql学习(七)-索引学习
常规索引: 在常用查询的字段上使用常规索引 创建表时一块创建索引 create table if not exists carshop(id int not null auto_increment, ...
- jquery easyUI DataGrid 初始化的时候就显示可排序的字段
在使用easy ui的列表中,想要标记可以排序的字段,使用户一看页面就知道哪些是可以点击排序的. 给可排序的字段添加 图标在列名后面.不可排序的字段还和原来一样. 步骤: 你需要一个图标 , 你需要给 ...
- redis安装方法
redis安装方法1.通过lnmp一键安装包,然后执行./addons.sh install redis2.yum -y install redis3.wget http://redis.google ...
- text-overflow: ellipsis;省略号颜色设置
参考stackoverfolow 想要给元素添加text-overflow: ellipsis;样式,必须保证改其父元素是块级元素,如果其是行级元素,可以通过设置display:block;或者dis ...