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.

题意:

不准乘除,不准膜。

Solution1:用加减和位运算。

乘法可以通过不断加法来获得

故,除法可以通过不断减法来获得

碎碎念:

数学渣对这种没有算法含量,又要大量数学sense的题,好抓狂。

发挥文科生优势。背诵吧!!

code

 class Solution {
public int divide(int dividend, int divisor) {
// corner case
if(dividend == 0) return 0; // 当 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, division ...

  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. PythonStudy——列表的常用操作 List of common operations

    # 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...

  2. alert大法看执行流程(一次采坑)

    页面的dom元素加载完了,给元素一次性添加事件. 收获:事件都是一次性给添加好的,不是点击一次,................................................... ...

  3. 深入Session2

    一.分布式环境Session的处理方法 分布式环境下要保持会话跟踪最简单的方式是只依靠客户端Cookie保存,不过大多数情况下还需要用到Session,一般的处理方式如下: 1.Session复制 每 ...

  4. C# datagridview大小跟随窗口动态改变(转)

    在C#中使用winform布局的时候,拖一个datagridview到窗体上面,将datagridview调整为适合窗体的大小. 但是运行之后,点击最大化按钮的时候,发现datagridview的大小 ...

  5. C# Interview Question 1

    What is C#? What's the feature of C# language? Answer:  

  6. Matlab中调用VS编译的exe文件并传递变量 的方法

    经历::在网上找了很多方法,都没有实现在matlab中调用vs的exe文件并且能够传递变量参数,一些小细节花费了自己很多时间,比喻忽略了一些空格!  网上很多的方法都是纯粹复制别人的方法,自己都没有去 ...

  7. JavaScript中的匿名函数遇上!会怎么样

    通常,我们声明一个函数test){},可以通过test()来调用这个函数.但是,如果我们在这个函数声明的末尾加上(),解析器是无法理解的. function test(){ console.log(' ...

  8. Flask--(一对多demo)作者书籍模型

    一对多模型的增加和删除 后端实现: from flask import Flask from flask import flash from flask import redirect from fl ...

  9. Flask--(一对多)模型渲染表单数据

    模型建立一一对多模型: 多表添加外键,建立两张表之间的关系 一表关联多表的属性,可以方便快速访问多表的数据 模板一层循环渲染一表数据,二层循环渲染多表的数据 代码展示: from flask impo ...

  10. Windows系统中监控文件复制操作的几种方式

    http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...