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

If it is overflow, return 2147483647

Have you met this question in a real interview?

 
 
Example

Given dividend = 100 and divisor = 9, return 11.

LeetCode上的原题,请参见我之前的博客Divide Two Integers

解法一:

class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
if (divisor == || (dividend == INT_MIN && divisor == -)) return INT_MAX;
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
if (n == ) return sign == ? m : -m;
while (m >= n) {
long long t = n, p = ;
while (m >= (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
return sign == ? res : -res;
}
};

解法二:

class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
if (m < n) return ;
while (m >= n) {
long long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};

解法三:

class Solution {
public:
/**
* @param dividend the dividend
* @param divisor the divisor
* @return the result
*/
int divide(int dividend, int divisor) {
long long m = abs((long long)dividend), n = abs((long long)divisor), res = ;
if (m < n) return ;
long long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p + divide(m - t, n);
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};

[LintCode] 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] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  3. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  4. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  5. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  6. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. [Swift]LeetCode29. 两数相除 | Divide Two Integers

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  9. LeetCode OJ:Divide Two Integers(两数相除)

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

随机推荐

  1. ios本地推送

    #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate //无论程序在 ...

  2. DIV伸缩盒子box

    <div class="div1"> <div class="box"> <div>A</div> <di ...

  3. Null值的使用

  4. 【转】Spark性能优化指南——基础篇

    http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&sn=2f413828d1fdc6a ...

  5. UVA 12232 Exclusive-OR(并查集+思想)

    题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...

  6. 重写ViewPager方法,防止滑动广告尾页的时候,Fragment也改变! (如果广告设置为轮播的话,不需要重写ViewPager)

    public class MyViewPager extends ViewPager{ public MyViewPager(Context context) { this(context, null ...

  7. python 类型判断-- isinstance函数

    判断类型 函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str.list.dict,也可以用在我们自定义的类,它们本质上都是数据类型. 假设有如下的 Per ...

  8. constructor

    function Person(name){ this.name = name; } Person.prototype = { constructor : Person, sayName : func ...

  9. 5、利用控件TVCLZip和TIdFTP压缩文件并上传到FTP的线程单元pas 改进版

    用到临界区 保护写日志的函数: 递归函数 删除目录下的所有文件: 循环创建或判断FTP的目录: 可改进的地方:循环压缩深层次目录的所以文件: 实现断点续传,或断点下载: {************** ...

  10. iris数据集

    iris以鸢尾花的特征作为数据来源,数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性,是在数据挖掘.数据分类中非常常用的测试集.训练集. 链接地址