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.

题目

不准乘除,不准膜。

思路

那剩下的还能用加减、位运算

代码

 // Divide Two Integers
// 时间复杂度O(logn),空间复杂度O(1)
public class Solution {
public int divide(int dividend, int divisor) {
if(dividend == 0) return 0;
if (divisor == 0) return Integer.MAX_VALUE; // 当 dividend = INT_MIN,divisor = -1时,结果会溢出
if (dividend == Integer.MIN_VALUE) {
if (divisor == -1) return Integer.MAX_VALUE;
else if (divisor < 0)
return 1 + divide(dividend - divisor, divisor);
else
return - 1 + divide((dividend + divisor), divisor);
}
if(divisor == Integer.MIN_VALUE){
return dividend == divisor ? 1 : 0;
} int a = dividend > 0 ? dividend : -dividend;
int b = divisor > 0 ? divisor : -divisor; int result = 0;
while (a >= b) {
int c = b;
for (int i = 0; a >= c;) {
a -= c;
result += 1 << i;
if (c < Integer.MAX_VALUE / 2) { // prevent overflow
++i;
c <<= 1;
}
}
} return ((dividend^divisor) >> 31) != 0 ? (-result) : (result);
}
}

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

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

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

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

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

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

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

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

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

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

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

  6. [LeetCode] 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. 029 Divide Two Integers 两数相除

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

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

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

随机推荐

  1. Linux下XAMPP装完之后,Navicat无法连上数据库的问题的解决 注意'mypassword'是当前的mysql登录密码

    Linux下装完XAMPP之后,mysql是自带装好了的,这个时候,mysql的root用户没有密码. 在mac 下安装好xampp后,需要在终端命令行操作时,比如输入:mysql -u root - ...

  2. tensorflow定义神经网络损失函数MSE

    import numpy as np import tensorflow as tf y_pred = np.array([[1], [2], [3]],dtype=np.float32) y_rea ...

  3. Solr学习总结(六)solr的函数查询Function Queries

    摘要: 函数查询允许你使用一个或多个数字字段的真实值生成一个相关性分数,函数查询在standard,DisMax,eDisMax下都能使用. 查询函数可以是常量,字段或者其他函数的组合.使用函数可以影 ...

  4. 【转载】全栈工程师-Hadoop, HBase, Hive, Spark

    学习参考这篇文章: http://www.shareditor.com/blogshow/?blogId=96 机器学习.数据挖掘等各种大数据处理都离不开各种开源分布式系统, hadoop用于分布式存 ...

  5. 本地yum源快速创建

    1.建立挂载目录mkdir /rui 2.挂载iso到新建的/rui目录

  6. PHP大小写是否敏感问题的汇总

      一.大小写敏感1. 变量名区分大小写view sourceprint?     <?php    $abc = 'abcd';    echo $abc; //输出 'abcd'     e ...

  7. leetcode108

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  8. tp5 redis 单例模式 转载

    单例模式(Singleton Pattern 单件模式或单元素模式) 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下3个特点: 1 . 它必须有一个构造函数, ...

  9. JPA和Hibernate到底是什么关系???

    转自:https://www.cnblogs.com/mosoner/p/9494250.html 在学习框架的过程中,发现学的东西很多,但是感觉他们之间的联系区别都不是很了解,知道JPA可以去实现持 ...

  10. FireFox 书签 缓存 路径设置

    English ver down https://www.mozilla.org/en-US/firefox/new/ add ons https://addons.mozilla.org/en-US ...