LeetCode29 Divide Two Integers
题目:
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的更多相关文章
- 算法练习--LeetCode--29. Divide Two Integers
Divide Two Integers Given two integers dividend and divisor, divide two integers without using multi ...
- [Swift]LeetCode29. 两数相除 | Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- asp web api 怎么使用put和delete。
Method Overriding RESTful services allow the clients to act on the resources through methods such as ...
- oracle数据库建表
create or replace directory dumpdir as 'E:\oracle\dumpdir';create temporary tablespace ydxt_temp tem ...
- 通过Microsoft Azure服务设计网络架构的经验分享(转)
原文:http://www.infoq.com/cn/articles/azure-networking-tips 本文从产品设计和架构角度分享了 Microsoft Azure 网络服务方面的使用经 ...
- CTE初识
微软从SQl2005起引入了CTE(Common Table Expression)以强化T-SQL.这是一个类似于非持久视图的好东西. 正常的SQL语句: select * from person. ...
- java -X 这不是标准的选项只是为了获取帮助信息
-? -help 输出此帮助消息 获取帮助信息方式有三种: java java -? java -help -X 输出非标准选项的帮助 java -X -Xms< ...
- struts2 下载时报java.lang.IllegalStateException
-----------------------------------------struts2 下载时报java.lang.IllegalStateException---------------- ...
- JVM启动参数小结
一:JVM启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),指的是JVM底层的一些配置参数, ...
- log4net配置的两种方式
----------文件配置:------------------- <?xml version="1.0" encoding="utf-8" ?> ...
- 转载:mysql update更新带子查询的实现方式
出自:http://576017120.iteye.com/blog/1947154 mysql中更新时不能直接将更新的表作为查询的表,可以通过临时中间表的形式. 总结一下: 一:单表更新时 例如: ...
- jQuery(一)delegate() 方法
定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 delegate() 方法的事件处理程序适用于当前或未来 ...