题目:

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. Delphi Refactor 重构

    delphi refactor procedure TCameraComponentForm.btnRefreshConfigClick(Sender: TObject); var a:string; ...

  2. 转】Mahout推荐算法API详解

    原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...

  3. HDU 1002 分类: ACM 2015-06-18 23:03 9人阅读 评论(0) 收藏

    昨天做的那题其实和大整数相加类似.记得当初我大一寒假就卡在这1002题上,结果之后就再也没写题... 到今天终于把大整数相加写了一遍. 不过写的很繁琐,抽时间改进一下写简洁一点. #include&l ...

  4. UVALive 7455 Linear Ecosystem (高斯消元)

    Linear Ecosystem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/B Description http://7xj ...

  5. Working with Sprites

    [Working with Sprites] 1.An SKSpriteNode object can be drawn either as a rectangle with a texture ma ...

  6. tomcat的host配置

    本机 etc\hosts 首先了解C:\WINDOWS\system32\drivers\etc\hosts文件配置 127.0.0.1 static1.ezsins.com #adoble ps c ...

  7. login placeholder

    $(function(){ function isPlaceholder(){ var input = document.createElement('input'); return 'placeho ...

  8. HDU1001

    求和 #include<stdio.h> int main() { long n; while(scanf("%ld",&n)!=EOF){ long i; ; ...

  9. 【转】android onNewIntent()触发机制及注意事项

    一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestro ...

  10. UDP套接口编程

    常用的UDP实现的程序:DNS域名系统,NFS网络文件系统,SNMP简单网络管理协议 ssize_t recvfrom(int sockfd,void *buff,size_t nbytes,int ...