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

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

Example 2:

Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

这道题让我们求两数相除,而且规定不能用乘法,除法和取余操作,那么这里可以用另一神器位操作 Bit Manipulation,思路是,如果被除数大于或等于除数,则进行如下循环,定义变量t等于除数,定义计数p,当t的两倍小于等于被除数时,进行如下循环,t扩大一倍,p扩大一倍,然后更新 res 和m。这道题的 OJ 给的一些 test case 非常的讨厌,因为输入的都是 int 型,比如被除数是 -2147483648,在 int 范围内,当除数是  -1 时,结果就超出了 int 范围,需要返回 INT_MAX,所以对于这种情况就在开始用 if 判定,将其和除数为0的情况放一起判定,返回 INT_MAX。然后还要根据被除数和除数的正负来确定返回值的正负,这里采用长整型 long 来完成所有的计算,最后返回值乘以符号即可,代码如下:

解法一:

class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == INT_MIN && divisor == -) return INT_MAX;
long m = labs(dividend), n = labs(divisor), res = ;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
if (n == ) return sign == ? m : -m;
while (m >= n) {
long t = n, p = ;
while (m >= (t << )) {
t <<= ;
p <<= ;
}
res += p;
m -= t;
}
return sign == ? res : -res;
}
};

我们可以通过递归的方法来解使上面的解法变得更加简洁:

解法二:

class Solution {
public:
int divide(int dividend, int divisor) {
long m = labs(dividend), n = labs(divisor), res = ;
if (m < n) return ;
long t = n, p = ;
while (m > (t << )) {
t <<= ;
p <<= ;
}
res += p + divide(m - t, n);
if ((dividend < ) ^ (divisor < )) res = -res;
return res > INT_MAX ? INT_MAX : res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/29

参考资料:

https://leetcode.com/problems/divide-two-integers/

https://leetcode.com/problems/divide-two-integers/discuss/13524/summary-of-3-c-solutions

https://leetcode.com/problems/divide-two-integers/discuss/13407/C%2B%2B-bit-manipulations

https://leetcode.com/problems/divide-two-integers/discuss/142849/C%2B%2BJavaPython-Should-Not-Use-%22long%22-Int

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 29. Divide Two Integers 两数相除的更多相关文章

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

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

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

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

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

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

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

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

  5. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

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

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

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

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

  8. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  9. 【LeetCode每天一题】Divide Two Integers(两整数相除)

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

随机推荐

  1. 《Spring + MyBatis 企业应用实战》书评

    最近公司的前端用 MpVUE.JS 开发微信小程序遇到一个问题,对后端传来的富文本编辑器的标签无法进行解析.因为公司小,这个问题前端人员直接反映给老板,跟老板说,“ MpVUE.JS 无法解析富文本编 ...

  2. centos6利用cgroup冻结一个程序运行

    操作步骤: 安装cgroup服务 yum install libcgroup 配置cgroup vim /etc/cgconfig.conf group stopit{ #添加一个cgroup组 fr ...

  3. CentOS系统安装Python3

    准备: CentOS 6.4系统 Python-3.6.5.tgz 下载地址: 官网:https://www.python.org/downloads/release/python-365/ 镜像:h ...

  4. STorM32 BGC三轴增稳云台驱动下载

    STorM32 BGC是一种硬件开源.软件闭源的三轴稳定云台控制项目.云台在我们生活中是越来越常见,我们手机拍照用的手持云台,无人机上挂载摄像机的机载隔振云台.我们在电影<流浪地球>里面那 ...

  5. mask-rcnn代码解读(七):display(self)函数的解析

    如和将class中定义的变量打印或读取出来,受maskrcnn的config.py的启示,我将对该函数进行解释. 我将介绍该函数前,需要对一些名词进行解释,如下: ①Ipython:ipython是一 ...

  6. 五分钟看懂UML类图与类的关系详解

    在画类图的时候,理清类和类之间的关系是重点.类的关系有泛化(Generalization).实现(Realization).依赖(Dependency)和关联(Association).其中关联又分为 ...

  7. wsl下安装并运行Kafka

    0.引言 kafka是一个高性能分布式的MQ,今天我们就来玩玩 1.安装 wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.0/kaf ...

  8. Laravel 运行php artisan serve命令时提示No application encryption key has been specified

    创建了新的laravel项目后, 运行提示:No application encryption key has been specified 解决方法: 这个是由于没有配置好 APP_KEY 在终端上 ...

  9. 3. 移动安全渗透测试-(Android基础漏洞)

    3.1 数据存储漏洞 用户经常会把敏感数据交给app,比如:用户名and密码认证令牌联系人记录通信记录历史使用记录..... 只要愿意,app可以收集这些用户的隐私和个人信息明文存储或明文传输,通常保 ...

  10. 苹果审核之遇到IPV6问题被拒的解决方法

    情景: 等待苹果审核上线时,发现因为IPV6被拒了.这是悲剧,以下是苹果审核给我的理由: We discovered one or more bugs on Wi-Fi connected to an ...