Divide Two Integers

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

思路: 类同 趣味算法之数学问题:题4.

两点需要注意: 1. 除数或被除数为最大负数时,转化为正数会溢出。2. divisor + divisor 可能会溢出。

class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor == 0) return INT_MAX;
bool signal = false;
bool overflow = false;
if(dividend < 0) {
signal = !signal;
if(dividend == INT_MIN) { overflow = true; dividend++; }
dividend *= -1;
}
if(divisor < 0) {
signal = !signal;
if(divisor == INT_MIN) {
if(overflow) return 1;
else return 0;
}
divisor *= -1;
}
int result = 0;
while(dividend >= divisor) {
int x(divisor);
int r(1);
while(dividend-x >= x) {
x += x;
r += r;
}
dividend -= x;
result += r;
}
if(overflow && dividend +1 == divisor) result++;
return signal ? (-result) : result;
}
};

62. Divide Two Integers的更多相关文章

  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 without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  3. leetcode-【中等题】Divide Two Integers

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

  4. [LintCode] Divide Two Integers 两数相除

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

  5. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  6. Java for LeetCode 029 Divide Two Integers

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

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

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

  8. LeetCode29 Divide Two Integers

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

  9. 【leetcode】Divide Two Integers (middle)☆

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

随机推荐

  1. CSU 1325 莫比乌斯反演

    题目大意: 一.有多少个有序数对(x,y)满足1<=x<=A,1<=y<=B,并且gcd(x,y)为p的一个约数: 二.有多少个有序数对(x,y)满足1<=x<=A ...

  2. 关于ADDED_TO_STAGE事件

    可视类初始化的时候,很多时候要用到stage属性,则要使用Event.ADDED_TO_STAGE事件,这个swf被其它的文件加载,如果直接在初始化函数内使用stage属性 .但是,文档类初始化函数内 ...

  3. MS Sql server 2008 学习笔记

    数据库中常用的概念 Sql本身是一个服务器,没有界面,Management Studio  只是一个SQL Server管理工具而已,不是服务器. Sql server 在管理工具下面的服务SQL S ...

  4. UE4 - C++ 射线捕捉

    #include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h" //省略大部分代码 void AMyFPS_Charact ...

  5. 【Python】实现5!+4!+3!+2!+1!

    #!/usr/bin/env python #-*- coding:utf-8 -*- def factorial_add(n): empty_list=[] #定义一个空列表 for i in ma ...

  6. 关于在listView中优化的问题 更多方

    面试官 说 需要至少说出8种 (--) 1.convertView的复用,切忌每次getView()都新建.listView的核心原理就是重用view. 2.ViewHolder 减少findView ...

  7. Qt开发环境中使用报表控件FastReport遇到的一些问题(一)

    FastReport是一款优秀的报表控件,由俄罗斯开发,国内有代理商.Qt环境下可实现报表功能的还有一款叫NCReport,也是收费控件,比较来比较去还是觉得前者功能强点.网上讲解此控件在Qt中使用的 ...

  8. JS运动基础(二) 摩擦运动、缓冲运动

    摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...

  9. EasyUI filebox组件在IE下不兼容

    EasyUI 1.4.1 jQuery v1.11.1 EasyUI1.4.1版本的filebox在IE9+环境下,提交表单上传文件时出错,不能使用.

  10. mysql取代rand()的高效率随机读取方法

    SELECT * FROM `table` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `table`)) AS id) AS t2 ...