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

Java实现:

位操作Bit Operation,思路是:如果被除数大于或等于除数,则进行如下循环,定义变量t等于除数,定义计数p,当t的两倍小于等于被除数时,进行如下循环,t扩大一倍,p扩大一倍,然后更新res和m。

class Solution {
public int divide(int dividend, int divisor) {
int res=0;
if(divisor==0){
return Integer.MAX_VALUE;
}
if(dividend==Integer.MIN_VALUE&&divisor==-1){
return Integer.MAX_VALUE;
}
long m=Math.abs((long)dividend);
long n=Math.abs((long)divisor);
while(m>=n){
long t=n,p=1;
while(m>=(t<<1)){
t<<=1;
p<<=1;
}
res+=p;
m-=t;
}
if((dividend>0 && divisor>0)||(dividend<0 && divisor<0)){
return res;
}else{
return -res;
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4431949.html

029 Divide Two Integers 两数相除的更多相关文章

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

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

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

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

  3. [LintCode] Divide Two Integers 两数相除

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

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

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

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

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

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

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

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

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

  8. [Swift]LeetCode29. 两数相除 | Divide Two Integers

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

  9. LeetCode OJ:Divide Two Integers(两数相除)

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

随机推荐

  1. Chrome Developer Tools之内存分析

    可参考: Chrome Developer Tools之内存分析 http://www.kazaff.me/2014/01/26/chrome-developer-tools%E4%B9%8B%E5% ...

  2. 代码实现跟控制器跳转到storyBoard

  3. CentOS6和CentOS7服务开机启动

    CentOS 6和CentOS 7都可以定义开机启动哪些服务,但CentOS 6的命令是chkconfig,CentOS 7是systemctl. 本文将介绍两种命令的实现方式. 一.CentOS 6 ...

  4. http请求中的get和post的区别

    1.标准答案 GET在浏览器回退时是无害的,而POST会再次提交请求. GET产生的URL地址可以被Bookmark,而POST不可以. GET请求会被浏览器主动cache,而POST不会,除非手动设 ...

  5. 使用jpa报No query defined for that name错误

    今天使用jpa创建本地查询时出现Java.lang.IllegalArgumentException: No query defined for that name..... 一个很sb的问题,调用e ...

  6. 如何从光盘本地安装CentOS 7图形界面(Gnome GUI)

    本例中通过在CentOS 7中修改repo文件,直接从光盘或者ISO镜像文件安装Gnome图形界面(Gnome GUI),从而避免耗时从官网或镜像下载. 1.首先确保光盘或者ISO镜像文件正确连接到客 ...

  7. NASA的CTO——开源软件使我们诚实

    Chris C.Kemp,谷歌设置新职位CTO让他领导 原文: NASA's CTO: Open source software keeps us honest 作者: Shawn Freeman 译 ...

  8. 32、Differential Gene Expression using RNA-Seq (Workflow)

    转载: https://github.com/twbattaglia/RNAseq-workflow Introduction RNAseq is becoming the one of the mo ...

  9. range分表

    按主键范围进行分表,假设当前主键的最大值为5000,把数据拆成5等份 1-1000:是5000的第1个范围,把该范围的数据作为一张表 1001-2000:是5000的第2个范围,把该范围的数据作为一张 ...

  10. Entity Framework Code-First(9.1):DataAnnotations - Key Attribute

    DataAnnotations - Key Attribute: Key attribute can be applied to properties of a class. Default Code ...