不使用乘号,除号和取模符号将两数相除。
如果溢出返回 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. fastcgi(一)

    首先安装 fastcgi 开发包 ... #wget http://www.fastcgi.com/dist/fcgi-current.tar.gz #tar -zxvf fcgi-current.t ...

  2. 从setTimeout到浏览器线程机制

    看高性能javascipt 这本书时,看到这么一句话: Putting scripts at the top of the page in this way typically leads to a ...

  3. NuGet管理

    使用NuGet管理项目类库引用   NuGet 是微软开发平台(包括.NET平台)的一个包管理器,这里只介绍和.NET相关的NuGet Visual Studio扩展客户端, 在VS2010 ,VS2 ...

  4. 关于socket编程获取客户端地址笔记

    因为最近刚好碰到这块,而且很不小心的在上面踩了个坑,所以把这个坑记录下来 首先,在我们都是在accept函数以后来获取客户端的地址: client_sd = accept(watcher->fd ...

  5. MySQL 实现调用外部程序和系统命令

    MySQL 实现调用外部程序和系统命令 Refer:http://www.cnblogs.com/yunsicai/p/4080864.html1) Download lib_mysqludf_sys ...

  6. Leetcode 8 Two Pointers

    Two Pointers 1. 28. Implement strStr() 用 i 记录haystack偏移量,j 记录 needle 的偏移量. class Solution { public i ...

  7. js 控制 class 类名(classList) 和 自定义属性(dataset)

    classList 用法: const div = document.querySelector('div') div.classList.add('myclass') // 添加类名 div.cla ...

  8. LodopFuncs.js和CLodopFuncs.js区别和联系

    所在位置:LodopFuncs.js可以在官网下载中心综合版里下载到.CLodopfuncs.js在C-Lodop服务缓存中,C-Lodop启动的时候才能访问到. 需不需要下载放置到项目里:(客户端本 ...

  9. java Map按Key排序

    public static void main(String[] args) { Map<String, String> map = new TreeMap<String, Stri ...

  10. iText5报表_页眉与页脚

    1.概述       iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作.PdfPageEventHelper ...