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 ...
随机推荐
- 1415-2个人项目Individual Project
作业要求: 个人独立完成,实践PSP相关知识. 时 间: 两周. (本来截止4月30日,考虑到刚迁移平台,延缓至5月7日) 实践目标: Github基本源代码控制方法 利用Junit4进行程序模块的测 ...
- JavaScript 数组的创建
数组定义:数组(array)是一种数据类型,它包含或者存储了编码的值,每个编码的值称作该数组的一个元素(element), 每个元素的编码被称作为下标(index). JavaScript一维数组创建 ...
- blocked file type by sharepoint 分类: Sharepoint 2015-07-05 07:45 6人阅读 评论(0) 收藏
o add or remove blocked file types by using Central Administration Verify that you have the followin ...
- web前端基础篇④
1.BFC-块级元素-块级格式化上下文布局规则:独立区域,与外部毫不相关内部box会在垂直方向,一个个放置box垂直方向距离由margin决定BFC的区域不会与float box重叠计算BFC高度时, ...
- MySQL高可用之MHA搭建
测试环境 节点1 172.16.200.231 6666 master 节点2 172.16.200.27 6666 slave1 ...
- C++ iostream的线程安全性问题
标准C里面的printf, fprintf之类的,会给FILE指针上锁(在unix平台上就是记录锁,按照msdn的说法windows上也有类似的锁),所以单次函数调用总是线程安全的: 要注意,这里只对 ...
- HDU5128 细心、细心、细心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给你n(n < 30)个点的坐标,然后让你求出这n个点能构成的两个最大矩形的面积,有 ...
- Nice Sequence_线段树***
Description Let us consider the sequence a1, a2,..., an of non-negative integer numbers. Denote as c ...
- nvcc fatal : Cannot find compiler 'cl.exe' in PATH解决方法
我在测试安装的deep learning工具theano.按照官网Baby Steps - Algebra一步步输入. >>> import theano.tensor as T & ...
- 分支语句 if的嵌套 循环语句
0930 今天学习内容做以下总结: 语句的分类:顺序语句,分支语句(选择,条件),循环语句 分支语句 格式1:if(表达式(要么是true 要么是false)){} 格式2:if(){}slse{} ...