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 ...
随机推荐
- Java中的异常处理
描述: 如果Java中的函数有可能抛出异常,则该异常要么被catch住,要么在声明函数时必须声明该函数体会throws exception. 处理的时候的流程是,当发生异常时,首先结束当前函数后续语句 ...
- log4net.config 单独文件
使用的命名空间下添加 [assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", Watch ...
- 黑马程序员——OC语言 类和对象
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)类 1)类的声明 代码编写 ①定义一个Car类,拥有2个属性:轮子数 ...
- 删除IE 下输入后的清除小叉叉
input::-ms-clear { display: none; } css去掉ie10,11中input[type="text"]后面的X图标 input[type=" ...
- oracle 解锁表
//查询锁表id select session_id from v$locked_object; //查询该ID的serial# SELECT sid, serial#, username, osus ...
- iOS 剪贴板基本知识
在iOS 中下面三个控件,自身就有复制-粘贴功能 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程序 ...
- CentOS上的RabbitMQ安装
1. erlang安装配置(这里我们在opt目录下进行安装配置) cd /opt 安装依赖文件: yum install gcc glibc-devel make ncurses-devel open ...
- C#与XML Schema的问题
http://bbs.csdn.net/topics/50493564 weileily: 用XmlSchema.Read方法读取了一个xsd文件,请问如何遍历检索器中的ComplexType与Sim ...
- 判断表字段是否存在default约束
sql语句 IF NOT EXISTS ( SELECT * FROM dbo.syscolumns WHERE id = OBJECT_ID('[dbo].[TActScoreReceiveRec] ...
- div滚动条弹出层效果 (所需要的css文件和js文件,都已经上传到文件里面了progressbar.rar)
<%--总的弹出层--%> <div class="tcck" id="joinclub" style="display:none& ...