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的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  3. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  4. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  6. Java for LeetCode 029 Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  7. [LeetCode] Divide Two Integers( bit + 二分法 )

    Divide two integers without using multiplication, division and mod operator. 常常出现大的负数,无法用abs()转换成正数的 ...

  8. LeetCode29 Divide Two Integers

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  9. 【leetcode】Divide Two Integers (middle)☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. WebService Ajax调用

    1.Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. springmvc学习第四天

    数据类型的转换.格式化.校验 1.数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFactory 实例,以创 ...

  3. 团队开发——冲刺1.c

    冲刺阶段一(第三天) 1.昨天做了什么? 在C#的Windows窗体应用程序中,设计简单的游戏界面. 2.今天准备做什么? 首先把昨天遇到的问题解决了,虽然没有找到原因,但是只要每一步修改后就立即运行 ...

  4. How to implement a custom type for NHibernate property

    http://blog.miraclespain.com/archive/2008/Mar-18.html <?xml version="1.0" encoding=&quo ...

  5. php-数据库访问--数据修改

    主页面元素修改脚本 <?php $code = $_GET["c"]; //造连接对象 $db = new MySQLi("localhost",&quo ...

  6. LeetCode()Minimum Window Substring 超时,但觉得很清晰。

    我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...

  7. (转)Let’s make a DQN 系列

    Let's make a DQN 系列 Let's make a DQN: Theory September 27, 2016DQN This article is part of series Le ...

  8. C# 两个集合对比获取不同

    public class CompareCollection { public List<string> CompareList(List<string> oldList, L ...

  9. LVS+Keepalived负载均衡配置

    简介 lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且可用性也没有前者高. lvs和keepalived组合使用后,配置lvs的VIP和负载均衡就都在ke ...

  10. 正常月报表年初未分配利润修改backup

    原来:GLQC('3132',K('年')-1,'12',,,'本币','N','','本币','0001')+GLQC('314115',K('年')-1,'01',,,'本币','N','','本 ...