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.

题目要求不使用乘除和取余的运算得到除法的结果,我们第一个想到的肯定是减法,除法的本质就是减法嘛,但是减法的时间复杂度很不友好,如果被除数是Integer.MAX_VALUE,除数是1,那就很麻烦了

在这个基础上我们想到了更高级一点的操作方法,位移运算,<<1表示把字节左移一位相当于*2,我们不断扩大除数divisor,直到它再扩大于被除数,这个时候我们需要缩小被除数,就用这个被除数减去当前的除数然后再重复之前的过程,就得到了我们想要的结果。这里需要注意的是测试样例存在integer.MAX_VALUE和-1的特殊情况,所以我们要使用long来帮助存储判断

class Solution {
public int divide(int dividend, int divisor) {
long m=Math.abs((long)dividend);
long n=Math.abs((long)divisor);
long res=0,tag=Integer.MAX_VALUE;
if(m<n) return 0;
while(m>=n){
long temp=n,count=1;
while(m>(temp<<1)){
temp<<=1;
count<<=1;
}
m-=temp;
res+=count;
}
if((dividend<0)^(divisor<0)) res=-res;
return (int)(res>tag ? tag:res);
}
}

[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, divisio ...

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

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

  4. [LeetCode] Divide Two Integers 两数相除

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

  5. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  6. [LintCode] 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. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

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

  9. 【LeetCode每天一题】Divide Two Integers(两整数相除)

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

随机推荐

  1. 【FAQ】Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet

    原因: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  2. 【FAQ】服务下线

    原因:磁盘已满

  3. numpy.histogram 官方手册

    numpy.histogram numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None) Co ...

  4. python基础之格式化字符串

    一.格式化字符功能介绍 应用场景:一般在print的时候提供占位符;python中提供两种格式化字符串方式:第一种是古老的利用百分号的方式,第二种是增强的格式化字符串.format 函数. 二.古老的 ...

  5. vmware vSphere虚拟网络(一)

    为了更好的了解vSphere网络虚拟化解决方案,这里引入了一些概念,以便我们更好的了解虚拟网络. 一.网卡: 物理网卡称为vmnic,在ESXi中,第一块物理网卡叫做vmnic0,第二块叫做vmnic ...

  6. ubutu16.04修改分辨率

    http://blog.csdn.net/oiken/article/details/71088230 vmware不能自动适应Ubuntu16.04的分辨率,而且Ubuntu16.04的displa ...

  7. kotlin spring @value 注解

    spring boot和kotlin里静态类使用@Value注解配置解决方案前言spring boot里默认是不能给静态属性使用@Value赋值的.所以这里使用中间变量过渡绑定. 方案//applic ...

  8. C++_类和动态内存分配1—动态内存和类

    静态类成员 num_strings成员声明为静态存储类.静态类成员有一个特点:无论创建了多少对象,程序都只创建一个静态类变量副本.也就是说,类的所有对象共享一个静态成员.num_strings成员可以 ...

  9. hdu6437 Videos 费用流

    题目传送门 题目大意: 给出n,每天有n个小时.有m种电影,每个电影有开始时间和结束时间,和01两种种类,k个人,每一部电影只能被一个人看,会获得一个快乐值wi,如果一个人连续看两部相同种类的电影,快 ...

  10. ECharts 雷达图怎么在类目值下面显示数值

    需要实现的效果: 官网里面的demo显示数值,都是在拐点处: [解决] 1.只显示类目 <div id="mychart" style="width:300px;h ...