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. php如何判断IP为有效IP地址

    不需要正则表达式来判断,因为在php5.2.0之后,有专门的函数来做这个判断了. 判断是否是合法IP if(filter_var($ip, FILTER_VALIDATE_IP)) { // it's ...

  2. php array_flip() 删除数组重复元素——大彻大悟

    1. php array_flip() 删除数组重复元素,如果用于一维索引数组,好理解. [root@BG-DB:~]$more arr.php  <?php         $arr = ar ...

  3. 【BZOJ】4517 [Sdoi2016]排列计数(数学+错排公式)

    题目 传送门:QWQ 分析 $ O(nlogn) $预处理出阶乘和阶乘的逆元,然后求组合数就成了$O(1)$了. 最后再套上错排公式:$ \huge d[i]=(i-1) \times (d[i-1] ...

  4. 【BZOJ】1085 [SCOI2005]骑士精神(IDA*)

    题目 传送门:QWQ 分析 我好菜啊. 一波IDA*水过去了. 代码 #include <bits/stdc++.h> using namespace std; ; char s[maxn ...

  5. Z-tree 统计每一父节点的叶子节点数(看这一篇就够了)

    最近刚走出校园的我找到了第一份工作,在入职考核中就遇见了一道Z-tree的试题 这道题目本身是不难的,但是我第一次接触这个插件而且还把解决问题的方向搞错了,弄的我好几天都很难受. 弄得我都开始怀疑人生 ...

  6. python_基础算法

    本篇主要实现九(八)大排序算法,分别是冒泡排序,插入排序,选择排序,希尔排序,归并排序,快速排序,堆排序,计数排序.希望大家回顾知识的时候也能从我的这篇文章得到帮助. 概述 十种常见排序算法可以分为两 ...

  7. ElasticSearch Document API

    删除索引库 可以看到id为1的索引库不见了 这里要修改下配置文件 slave1,slave2也做同样的操作,在这里就不多赘述了. 这个时候记得要重启elasticseach才能生效,怎么重启这里就不多 ...

  8. virtualenv之python虚拟环境

    virtualEnv可以方便的解决不同项目中,对类库的依赖问题.首先将常用的类库安装在系统环境中,然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环境中. virtualen ...

  9. 技术思维VS管理思维

    以下为技术思维与管理思维的不同 在日常的工作中,会出现身兼两职 开发和项目经理 的情况,在此就要学会游刃有余的切换角色,方能一人分身二角 角色转换本质上是思维转换.思维决定一个人的行为,项目经理不像项 ...

  10. case功能菜单选项

    脚本aim; 实现如下功能菜单并实现相应的功能;同在之前公司的一个游戏控制脚本; 1),显示当前时间; 2),显示cpu负载 3),显示剩余内存 0),退出脚本 如图菜单界面