题目:

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

If it is overflow, return MAX_INT. (Medium)

分析:

题目要求不使用乘除和模运算实现两个整数除法。

第一个思路就是每次把count加等被除数自身判定,只到count<=除数,并且count + 被除数 > 除数时即为结果。

但是考虑到可能有 MAX_INT / 1这种情况,肯定华丽超时。

然后考虑使用移位运算,每次将count加等被除数左移一位(*2),满足条件后跳出循环,并且把除数 -= count,再来,只到除数 < 被除数挑出外循环。

注意:

这种数学题不是很好写(从AC率只有15%左右可以看出)。除了想清楚算法本身,

还要注意正负数处理,注意int范围处理(一般改成long long最后再判定比较简便,比如reverse integer)

代码:

 class Solution {
public:
int divide(int dividend, int divisor) {
long long ldividend = dividend;
long long ldivisor = divisor;
int flag = ;
if (ldividend < ) {
ldividend = -ldividend;
flag = -flag;
}
if (ldivisor < ) {
ldivisor = -ldivisor;
flag = -flag;
}
long long result = ;
while (ldivisor <= ldividend) {
long long count = ldivisor;
long long temp = ;
long long tempDividend = ldividend;
while ( !(count <= tempDividend && (count << ) > tempDividend)) {
count <<= ;
temp <<= ;
}
result += temp;
ldividend -= count;
}
if (flag < ) {
if (result > 0x80000000) {
return 0x7FFFFFFF;
}
else {
return -result;
}
}
else {
if (result > 0x7FFFFFFF) {
return 0x7FFFFFFF;
}
else {
return result;
}
} }
};

LeetCode29 Divide Two Integers的更多相关文章

  1. 算法练习--LeetCode--29. Divide Two Integers

    Divide Two Integers Given two integers dividend and divisor, divide two integers without using multi ...

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

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

  3. [LeetCode] Divide Two Integers 两数相除

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

  4. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

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

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

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

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

  7. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

  8. Divide Two Integers leetcode

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

  9. Java for LeetCode 029 Divide Two Integers

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

随机推荐

  1. Ubuntu 创建开机自启动脚本的方法

    http://askubuntu.com/questions/9382/how-can-i-configure-a-service-to-run-at-startuphttp://stackoverf ...

  2. rabbitMQ 远程访问

    AMQP server localhost:5672 closed the connection. Check login credentials: Socket closed root@ruiy-c ...

  3. 2款好用的Web在线编辑器

    1.CKEditor FCKEditor 现在已经重新开发,并改名为 CKEditor. CKeditor是一个专门使用在网页上,开放源代码,高度可定制,跨平台的所见即所得文字编辑器,兼容于绝大部分的 ...

  4. 指令式Callback,函数式Promise:对node.js的一声叹息

    原文:Callbacks are imperative, promises are functional: Node's biggest missed opportunity promises 天生就 ...

  5. HDU 2897 邂逅明下 (简单博弈,找规律)

    邂逅明下 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. python抓取网页例子

    python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...

  7. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  8. Median of Two Sorted Arrays-----LeetCode

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  9. HDU 4499 Cannon (暴力求解)

    题意:给定一个n*m个棋盘,放上一些棋子,问你最多能放几个炮(中国象棋中的炮). 析:其实很简单,因为棋盘才是5*5最大,那么直接暴力就行,可以看成一行,很水,时间很短,才62ms. 代码如下: #i ...

  10. hdoj 5386 Cover

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...