1. 原题链接

https://leetcode.com/problems/divide-two-integers/description/

2. 题目要求

给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计。

注意:不能使用乘法、除法和取余运算

3. 解题思路

陷阱一:MIN_VALUE/-1会溢出。因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1

陷阱二:除数divisor不能等于“0”


思路一:使用一个while循环,当dividend >= divisor时,进入循环。dividend = divident - divisor,每减一次计数器res+1。循环结束后则得到二者之商。

缺点:时间复杂度为O( n ),当被除数很大、除数很小时,效率非常低

 public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
} public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvd >= dvs) {
dvd -= dvs;
res++;
}
return sign == 1 ? res : -res;
}
}

思路二:采用位移运算,任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基。然后接下来我们每次尝试减去这个基,如果可以则结果增加加2^k,然后基继续右移迭代,直到基为0为止。因为这个方法的迭代次数是按2的幂直到超过结果,所以时间复杂度为O(logn)

 public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
} public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvs <= dvd) {
long temp = dvs, mul = 1;
while (dvd >= temp << 1) { // temp<<1,二进制表示左移一位,等价于temp乘以2
temp = temp << 1;
mul = mul << 1;
System.out.println("temp = " + temp + " " + "mul = " + mul);
}
dvd -= temp;
System.out.println("dvd" + dvd);
res += mul;
}
return sign == 1 ? res : -res;
}
}

LeetCode: 29. Divide Two Integers (Medium)的更多相关文章

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

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

  2. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

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

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

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

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

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

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

  6. [LeetCode] 29. Divide Two Integers ☆☆

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

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

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

  8. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  9. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

随机推荐

  1. AAAI 2016 paper阅读

    本篇文章调研一些感兴趣的AAAI 2016 papers.科研要多读paper!!! Learning to Generate Posters of Scientific Papers,Yuting ...

  2. 记录 iOS 各种跳转到系统应用

    MARK ----拨打电话 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; if (phoneVersion.fl ...

  3. 使用jQuery实现option的上移和下移

    基本思路:     上移:(1)获取当前选中的元素的索引值             (2)判断当前元素是否为第一个元素             (3)如果是,则不执行上移操作,如果不是,则则调用ins ...

  4. [转]Asp.Net url中文乱码

    一般有3种方法: 1.设置web.config文件 2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码.  传递 string Name = "中文参数"; Resp ...

  5. UVALive4682 XOR Sum

    UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...

  6. 协议类接口 - SPI

     一.SPI概述 SPI(Serial Peripheral Interface,串行外设接口)总线系统是一种同步串行外设接口,它可以使CPU与各种外围设备以串行方式进行通信以交换信息.一般主控SoC ...

  7. Oracle 左连接(+)加号用法及常用语法之间的关系

    本文目的: 通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法 分析步骤: 1.首先创建测试表的结构: create table test_left_a (a ...

  8. php中的引用

    $var1 = 'zhuchunyu'; $var2 = ""; function foo($vaa){ global $var1,$var2; if (!$vaa){ $var2 ...

  9. Bootstrap Data Table简单使用(动态加载数据)

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  10. js浮点型,整型操作方法汇总(进行中)

    浮点数操作方法如下: 1. Math.ceil()用作向上取整.(ceil 天花板) 2. Math.floor()用作向下取整. (floor 地板) (js 中取整底层原理是位运算的取反~运算,运 ...