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. Redis 设计与实现,看 SDS(Simple Dynamic String) 感悟

    Redis 设计与实现,看 SDS(Simple Dynamic String) 感悟 今天在看 Redis 设计与实现这本书的时候,发现了里面系统定义的数据结构 SDS,中文名为 简单动态字符串.对 ...

  2. sql server 下载安装标记

    SQL Server 2017 的各版本和支持的功能 https://docs.microsoft.com/zh-cn/sql/sql-server/editions-and-components-o ...

  3. (转)dnSpy 强大的.Net反编译软件

    目录 1. Debug外部引用的Dll文件2. 调试应用程序3. 修改exe文件的内容 作者:D.泡沫 一说起.net的反编译软件,大家首先想到的就是Reflector,ILSpy,dotPeek等等 ...

  4. Kubernetes 部署Web UI (Dashboard)

    Kubernetes 部署Web UI (Dashboard) 项目下载地址:https://github.com/kubernetes/kubernetes/tree/master/cluster/ ...

  5. Microsoft.Practices.Unity

    // // Summary: // Register a type mapping with the container. // // Parameters: // container: // Con ...

  6. Java自学-集合框架 HashSet

    Java集合框架 HashSet 示例 1 : 元素不能重复 Set中的元素,不能重复 package collection; import java.util.HashSet; public cla ...

  7. web前端-框架jquery

    1.jquery库 就是js的库 ,可以通过jquery语法简化js操作 ,如文档遍历 ,文档操作 ,事件处理 ,动画js定时器等等 2.引用 下载:https://www.bootcdn.cn/jq ...

  8. maven 学习---使用Maven模板创建项目

    在本教程中,我们将向你展示如何使用mvn archetype:generate从现有的Maven模板列表中生成项目.在Maven 3.3.3,有超过1000+个模板,Maven 团队已经过滤掉一些无用 ...

  9. 将流数据输出到Mysql中

    outputMysqlApp.scala import java.sql.DriverManager import org.apache.spark.SparkConf import org.apac ...

  10. The listener supports no services oracle注册监听

    问题登场: [oracle@my-e450 ~]$ lsnrctl status …… The listener supports no servicesThe command completed s ...