[LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return 2147483647
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 两数相除的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [Swift]LeetCode29. 两数相除 | Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- 人性的弱点&&影响力
How wo win friends and influence people 人性的弱点 by 卡耐基 人际关系基本技巧 不要批评.谴责.抱怨 真诚的欣赏他人 激发他人的渴望 获得别人好感的方式 微 ...
- Salesforce中所有常用类型字段的取值与赋值
Salesforce中所有常用字段类型的定义以及如何用代码进行取值和赋值: Field Type的定义: http://www.salesforce.com/us/developer/docs/api ...
- MOS X 下Apache服务器配置,及日志读取
A01-配置Apache 在当前用户的目录创建一个文件夹 打开finder进入/etc/apache2/etc/apache2 是系统目录,默认不显示 进入该目录有两种方法 i. 显示所有隐藏和系统目 ...
- 如何离线下载Chrome的安装包
打开Chrome官网(自行搜索)点击下载后下载的是联网安装包,这对部分上网不方便的用户造成了一定的麻烦. http://www.google.cn/chrome/browser/desktop/ind ...
- Android 实现ListView中Item被单击后背景色保持高亮
今天为了解决一个需求,就是我有一个slidingDrawer,里面是一个ListView.然后,单击其中的Item,默认只是显示一个橙色背景后就恢复了.客户便有着个需求,需要单击这个Item的背景高亮 ...
- 基于PHP以及Mysql,使用WordPress搭建站点
1.前提环境是PHP以及Mysql以及安装配置完成,Nginx服务启动: 2.配置Mysql的初始密码:mysql安装后,默认root密码是空的,所以要设置密码: mysql -u root mys ...
- Python与Hack之Unix口令
1.在实验时候,先导入crypt库:必须在Unix环境下才能实现这个模块 2.代码贴一下,以后有了Unix环境试试吧: import cryptimport syssys.modules['Crypt ...
- [xsd学习]xsd实例
以下为一个表示学校的xml文件,学校内有若干学生,每个学生都有基本信息,电脑信息,选课信息 <?xml version="1.0" encoding="UTF-8& ...
- js与jquery异同
大家都知道jquery是js的一个库,很多东西大多数简写了,让js写起来特别的方便.但是对与学习的人来说,最好是先学会了js再去学jquery会更好.在学得过程中你会发现两者实现的原理是差不多的,但是 ...
- 简单的实现UIpicker上面的取消确定按钮
1 因为我用的xib实现的添加picker 和textfiled的, @interface ViewController : UIViewController<UITextFieldDelega ...