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. D3——根据数据画图

    为DOM元素添加class .attr("class", "bar") //为元素添加class属性,<div class="bar" ...

  2. 软件架构的描述-Architecture Models

    Software architecture involves the high level structure of software system abstraction, by using dec ...

  3. 问题解决:java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date

    问题描述: 数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00” 程序使用select 语句从中取数据时出现以下异常: Java.sql.SQLE ...

  4. Thread-Specific-Storage for C/C++

    引用出处:https://www.cse.wustl.edu/~schmidt/PDF/TSS-pattern.pdf 摘要: 理论上多线程会提高程序性能,但实际上,由于在获取和释放锁的开销,多线程经 ...

  5. Centos7 搭建jupyter远程服务器

    前提:已经安装好jupyter 和Ipython,个人安装anaconda自带jupyter和Ipython 步骤1:生成配置文件: jupyter notebook --generate-confi ...

  6. PHP----练习-----新闻管理----增删改查

    练习-----新闻管理 题目要求如下: 做法: [1]建数据库 [2]封装类文件--------DBDA.class.php <?php class DBDA { public $fuwuqi= ...

  7. VS2013安装部署过程详解

    注意:缺少安装部署的小伙伴,看上一篇有详细介绍 程序在“Release”平台下编译运行没有错误 第一步:“新建”------“项目”------“其他项目类型”------“安装部署”------“I ...

  8. CSS布局方面的一些小总结

    1. display属性 display是CSS布局的第一站,它控制一个元素以什么“身份”出现在页面布局当中.它的值有很多个,常用的有block,inline,inline-block,table,n ...

  9. Faster Alternatives to glReadPixels and glTexImage2D in OpenGL ES

    In the development of Shou, I’ve been using GLSL with NEON to manipulate image rotation, scaling and ...

  10. Gradle Goodness: Unpacking an Archive

    To create an archive with Gradle is easy. We have several tasks like Zip, Tar, Jar, War and Ear to c ...