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. Excel转datatable

    如果想支持 .xls,.xlsx 两种格式 则必须安装一个exe文件,下载路径https://www.microsoft.com/zh-CN/download/details.aspx?id=1325 ...

  2. Linux初级入门(第一次作业)

    Linux初级入门 在本科期间学过一些Linux的简单命令,再次接触Linux不仅巩固了知识还学习到了很多新的东西. 什么是操作系统? 操作系统,英文名称Operating System,简称OS,是 ...

  3. [JAVA]对象的别名问题

    对于JAVA的基本数据类型,a=b就是把b的内容复制给a.若接着又修改了a,对b是没有影响的. 但是在为对象“赋值”的时候,情况发生了变化.对一个对象进行操作时,我们真正操作的是对象的引用. 下面对两 ...

  4. InnoDB引擎体系架构

    InnoDB引擎架构介绍 innodb存储引擎的体系架构,可简单划分成三层: 数据文件 :磁盘上的数据文件 内存池:缓存磁盘上的数据,方便读取,同时在对磁盘文件数据修改之前在这里缓存,然后按一定规刷新 ...

  5. springboot @RequestBody使用JsonSerialize与 JsonDeserialize自定义转参数,处理Date日期格式

    JsonDeserialize: 1.请求接收的是一个json,并且由对象接收 @RequestMapping(value = "/query", method = {Reques ...

  6. vagrant The specified host network collides with a non-hostonly network!

    换个ip scripts\homestead.rb config.vm.network :private_network, ip: settings["ip"] ||= " ...

  7. jQuery解决IE6/7/8不能使用 JSON.stringify 函数的问题

    原文地址:http://www.ynpxrz.com/n1445665c2023.aspx JSON 对象是在 ECMAScript 第 5 版中实现的,此版于 2009 年 12 月发布:IE6 I ...

  8. python之路——8

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 .1.文件操作 笔记.txt 1.文件路径:D:\python\Day8\笔记.txt 2.编码方 ...

  9. note 4 三大结构

    程序流程图 顺序结构 选择结构 if if-else if 语句-嵌套结构(Nested) 多分支结构(Chained) if score >= 90: print 'ARM' elif sco ...

  10. 01.JDBC操作数据库-快速入门操作

    /** * 简单入门操作 * 注:先将mysql-connector-java-5.1.36.jar 构建 Build Path环境当中去 * @param args * @throws Except ...