题目:

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. 第三百二十一天 how can I 坚持

    上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...

  2. delphi请求idhttp数据

    idhttp ss : TStringStream; begin ss := TStringStream.)); { 指定gb2312的中文代码页,或者54936(gb18030)更好些 utf8 对 ...

  3. RESTful API 设计最佳实践(转)

    摘要:目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API ...

  4. POP3&SMTP&IMAP

    [POP3&SMTP&IMAP] IMAP是什么? IMAP,即Internet Message Access Protocol(互联网邮件访问协议),您可以通过这种协议从邮件服务器上 ...

  5. Centos 64位安装 EPEL源

    #直接在线安装rpm包 rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm # ...

  6. Maven+Spring Batch+Apache Commons VF学习

    Apache Commons VFS资料:例子:http://www.zihou.me/html/2011/04/12/3377.html详细例子:http://p7engqingyang.iteye ...

  7. AJAX的工作原理及其优缺点

    1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...

  8. thinkphp 定位查询 Model:last您所请求的方法不存在!

    thinkphp 定位查询 Model:last您所请求的方法不存在!   用thinkphp3.1做项目的时候为了获取记录表中最后一个id用到了last()方法,出现了这个错误:Model:last ...

  9. aspnetpager的2种分页方法

    <webdiyer:AspNetPager ID="AspNetPager1" UrlPaging="True" PageSize="20&qu ...

  10. easyui grid中翻页多选方法

    <table class="easyui-datagrid" title="人员选择" id="dg" data-options=&q ...