62. Divide Two Integers
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
思路: 类同 趣味算法之数学问题:题4.
两点需要注意: 1. 除数或被除数为最大负数时,转化为正数会溢出。2. divisor + divisor 可能会溢出。
class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor == 0) return INT_MAX;
bool signal = false;
bool overflow = false;
if(dividend < 0) {
signal = !signal;
if(dividend == INT_MIN) { overflow = true; dividend++; }
dividend *= -1;
}
if(divisor < 0) {
signal = !signal;
if(divisor == INT_MIN) {
if(overflow) return 1;
else return 0;
}
divisor *= -1;
}
int result = 0;
while(dividend >= divisor) {
int x(divisor);
int r(1);
while(dividend-x >= x) {
x += x;
r += r;
}
dividend -= x;
result += r;
}
if(overflow && dividend +1 == divisor) result++;
return signal ? (-result) : result;
}
};
62. Divide Two Integers的更多相关文章
- [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 ...
- 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 ...
- [LeetCode] Divide Two Integers( bit + 二分法 )
Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...
- LeetCode29 Divide Two Integers
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- ASP.NET Web API 入门示例详解
REST服务已经成为最新的服务端开发趋势,ASP.NET Web API即为.NET平台的一种轻量级REST架构. ASP.NET Web API直接借鉴了ASP.NET MVC的设计,两者具有非常类 ...
- HTML新增属性
1.<input type="text" required必填 placeholder="默认显示内容" autofocus自动获取焦点 /> ...
- jquery元素插入、删除、清空
1)jquery元素插入 <!--位置1--> <div id='test'> <!--位置2--> <div>测试</div> <! ...
- socket协议下如何缓存图片--推荐EGOCache
EGOCache是一个轻量级的缓存框架.用法简单方便,在现在的项目中,我就用到EGOCache来缓存下载过的照片和字符串. 有人可能会问到,缓存照片还需要用EGOCache吗?AFNetworking ...
- linux上安装php+gd扩展
515 cd zlib-1.2.3 516 ./configure --prefix=/usr/local/zlib2 517 make && make install 518 cd ...
- OpenSSL - 网络安全之数据加密和数字证书
功能应用: 消息摘要,给文件或数据生成消息摘要,消息摘要只能校验数据的完整性,如SHA.MD5 数据加密和解密:对数据进行加密解密,OpenSSL实现了所有加密算法 数字证书:可以通过命令行或代码生成 ...
- Android 学习第5课,配置android
昨天与今天都在配置安卓的开发环境, 搞的头都大了,步骤比较多 1. 先下载安装 java 的 jdk ,这个是最基础的组件 2. 再下载 android SDK, http://developer. ...
- Python 基礎 - if else流程判斷
hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...
- Java问题总结
1.如何查看使用java的版本 cmd-->java -version 2.如何下载jdk,sdk Java.JDK(工具包)的安装_百度经验http://jingyan.baidu.com/a ...
- 1-Recyclerview使用系列之Recyclerview的列表数据显示
使用步骤已经写到我的公众号,二维码在下面,欢迎关注,谢谢. 本人联系方式: 更多精彩分享,可关注我的微信公众号: 若想给予我分享更多知识的动力,请扫描下面的微信打赏二维码,谢谢!: 微信号:Weixi ...