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:

  1. Input: dividend = 10, divisor = 3
  2. Output: 3

Example 2:

  1. Input: dividend = 7, divisor = -3
  2. 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 来完成所有的计算,最后返回值乘以符号即可,代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int divide(int dividend, int divisor) {
  4. if (dividend == INT_MIN && divisor == -) return INT_MAX;
  5. long m = labs(dividend), n = labs(divisor), res = ;
  6. int sign = ((dividend < ) ^ (divisor < )) ? - : ;
  7. if (n == ) return sign == ? m : -m;
  8. while (m >= n) {
  9. long t = n, p = ;
  10. while (m >= (t << )) {
  11. t <<= ;
  12. p <<= ;
  13. }
  14. res += p;
  15. m -= t;
  16. }
  17. return sign == ? res : -res;
  18. }
  19. };

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

解法二:

  1. class Solution {
  2. public:
  3. int divide(int dividend, int divisor) {
  4. long m = labs(dividend), n = labs(divisor), res = ;
  5. if (m < n) return ;
  6. long t = n, p = ;
  7. while (m > (t << )) {
  8. t <<= ;
  9. p <<= ;
  10. }
  11. res += p + divide(m - t, n);
  12. if ((dividend < ) ^ (divisor < )) res = -res;
  13. return res > INT_MAX ? INT_MAX : res;
  14. }
  15. };

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. 在 EF Core 中 Book 实体在新增、修改、删除时,给 LastUpdated 字段赋值。

    直接贴代码: public class MenusContext : DbContext { public static class ColumnNames { public const string ...

  2. C#网页 截图

    using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Threading; using S ...

  3. C#循环结构

    一.背景: 因编程的基础差,因此最近开始巩固学习C#基础,后期把自己学习的东西,总结相应文章中,有不足处请大家多多指教. 二.简介 有的时候,可能需要多次执行同一块代码.一般情况下,语句是顺序执行的: ...

  4. C#中的一些对话框问题处理

    1. 对于打开文件对话框处理 #region 打开文件对话框 string StrPath; OpenFileDialog Flag = new OpenFileDialog(); Flag.Mult ...

  5. Java实现QQ邮件发送

    首先我们需要两个jar包,点击下面即可下载这两个包: JavaMail mail.jar 1.4.5 JAF(版本 1.1.1) activation.jar 我们这里采用QQ邮箱发送邮件为例,代码如 ...

  6. office 所有后缀对应的 content-type

    后缀 MIME Type.doc application/msword.dot application/msword.docx application/vnd.openxmlformats-offic ...

  7. Webpack相关原理浅析

    基本打包机制 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(de ...

  8. Linux open fopen fdopen

    int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); 以 ...

  9. swift混编

    http://blog.csdn.net/fengsh998/article/details/34440159

  10. 二叉搜索树(BST)基本操作

    什么是二叉搜索树? 二叉搜索树也叫做二叉排序树.二叉查找树,它有以下性质: 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值: 若任意节点的右子树不空,则右子树上所有节点的值均大于它 ...