Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

思路I:做减法,直到被除数<除数。但结果 Time Limit Exceeded

class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor==) //分母为0
return INT_MAX; long int lDividend = dividend;
long int lDivisor = divisor;
long int quote=;
bool pos = (lDividend>= && lDivisor>) || (lDividend< && lDivisor<);
lDividend = abs(lDividend);
lDivisor = abs(lDivisor); while(lDividend >= lDivisor){
lDividend-=lDivisor;
quote++;
} if(pos && -quote==INT_MIN){
return INT_MAX;
}
return (int) pos?quote:(-quote);
}
};

思路II:任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=2^0+2^1+2^2+...+2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基,移动k位。接下来我们每次尝试减去这个基,如果可以则结果增加加2^k。然后基继续右移迭代,直到基<divisor为止。因为这个方法的迭代次数是按2的幂知道超过结果,所以时间复杂度为O(logn)。

class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor==) //分母为0
return INT_MAX; int res = ;
if(dividend==INT_MIN) //handle overflow
{
if(divisor==-)
return INT_MAX;
res = ;
dividend += abs(divisor);
}
if(divisor==INT_MIN) //handle overflow
return res; bool isNeg = ((dividend^divisor)>>!=)?true:false; //判断两数相乘除的结果
dividend = abs(dividend);
divisor = abs(divisor);
int digit = ; //标记除数乘了多少次2 //将除数向左移到最大
while(divisor<=(dividend>>)) //与被除数除2相比,为防止overflow
{
divisor <<= ;
digit++;
} while(digit>=)
{
if(dividend>=divisor)
{
dividend -= divisor;
res += <<digit; //除数左移了digit次,商就要加上2^digit
}
divisor >>= ;
digit--;
}
return isNeg?-res:res;
}
};

29. Divide Two Integers (INT; Overflow, Bit)的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  2. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  3. Java [leetcode 29]Divide Two Integers

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

  4. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  5. 29. Divide Two Integers (JAVA)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. [LeetCode] 29. Divide Two Integers ☆☆

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

  9. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

随机推荐

  1. [转载]java中的标号:outer的作用

    转载自:http://blog.sina.com.cn/s/blog_6f8bd746010136yr.html 标号label 标号提供了一种简单的break语句所不能实现的控制循环的方法,当在循环 ...

  2. Server JRE 简介

    Server JRE, 服务器版JRE JRE安装包, JDK安装包, 以及 Server JRE 压缩包, 在 Java SE Download 页面都可以下载: http://www.oracle ...

  3. HDU 1198

    http://acm.hdu.edu.cn/showproblem.php?pid=1198 裸并查集,主要工作在根据题目给出关系构图 #include <iostream> #inclu ...

  4. scikit-learn 学习笔记-- Generalized Linear Models (三)

    Bayesian regression 前面介绍的线性模型都是从最小二乘,均方误差的角度去建立的,从最简单的最小二乘到带正则项的 lasso,ridge 等.而 Bayesian regression ...

  5. centos7 中将执行文件python链接为python3后 如何保证 yum 功能不受影响

    1.  查看  /usr/bin  中  python 执行文件的 链接情况 2.  设置   python  命令的可执行文件  链接为    python3 3.  此时 , yum  文件中的p ...

  6. 浅谈SQL Server---2

    浅谈SQL Server内部运行机制 https://www.cnblogs.com/wangjiming/p/10098061.html 对于已经很熟悉T-SQL的读者,或者对于较专业的DBA来说, ...

  7. Python面试题(十六)

    1.取出两个升序数组中的最小的一半的元素组成一个新的升序数组. map(lambda x, y: x if x < y else y, a, b) 答案 2.用至少2种不同的方式删除一个list ...

  8. windows 如何查看端口占用进程ID 进程名称 强制结束进程

    1.查看指定端口的占用情况C:\>netstat -aon|findstr "9050" 协议    本地地址                     外部地址        ...

  9. haproxy 安装与配置

    一. Haproxy 介绍 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根据官方数据,其最高极限支持10G的并发.HAP ...

  10. jython研究笔记

    jython目前只支持python2,不支持python3. python中使用第三方包的话需要重新设置lib的地址. public void getHtmlByTxt(String path) { ...