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. vue.js 源代码学习笔记 ----- keep-alives

    /* @flow */ import { callHook } from 'core/instance/lifecycle' import { getFirstComponentChild } fro ...

  2. BZOJ5091: [Lydsy1711月赛]摘苹果(简单概率)

    5091: [Lydsy1711月赛]摘苹果 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 214  Solved: 163[Submit][Statu ...

  3. HDU 1847:Good Luck in CET-4 Everybody!(规律?博弈?)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. 6-20 No Less Than X in BST(20 分)

    You are supposed to output, in decreasing order, all the elements no less than X in a binary search ...

  5. LG3781 [SDOI2017]切树游戏

    题意 题目描述 小Q是一个热爱学习的人,他经常去维基百科学习计算机科学. 就在刚才,小Q认真地学习了一系列位运算符,其中按位异或的运算符\(\oplus\)对他影响很大.按位异或的运算符是双目运算符. ...

  6. 文档内容类似项处理-Shingling

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jituotianxia2009/article/details/25339807 集合的Jaccar ...

  7. table 如何给tr border颜色

    border-collapse属性值 说明 separate 默认值,边框分开,不合并 collapse 边框合并,如果相邻,则共用一个边框 table,th,td{border:1px solid  ...

  8. bzoj 5006(洛谷 4547) [THUWC2017]Bipartite 随机二分图——期望DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5006 https://www.luogu.org/problemnew/show/P4547 ...

  9. Linux下Moodle平台的快速安装方案

    一种快速安装与配置Moodle平台的方案,基本步骤: 1.选择与安装Linux系统 2.配置网络,开启shh和网络端口 3.一键安装集成环境(使用oneinstack方案) 4.服务器配置,端口和PH ...

  10. WebHttpBinding.ReaderQuotas 无法设置或者无法点出来

    项目需要引用System.Runtime.Serialization.dll 才能设置各项值: binding.ReaderQuotas.MaxDepth = ; binding.ReaderQuot ...