029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除。
如果溢出返回 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 两数相除的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [Swift]LeetCode29. 两数相除 | Divide Two Integers
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- Python:生成器函数
生成器函数:包含yield语句的函数: 生成器对象:生成器对象和迭代器对象行为相似,都支持可迭代接口:__next__(),若想执行生成器函数内部语句,则需要迭代协议’ A.生成器函数被调用时,并不会 ...
- python 接口测试字符类型转换
接口返回数据: print sql[0][10] {"recv_name":"rdgztest_63126","send_name":&qu ...
- jprofiler9.1.1 安装与配置
一.安装部分 安装包: 1.jprofiler_linux_9_1_1.rpm 2.jprofiler_windows-x64_9_1_1.exe 需要注意的是,Linux 和 Windows 安装的 ...
- 第3章 机器学习的典型应用 3-4 典型应用-ctr预估和协同过滤
ctr预估,用户点击率的预估.这个算法的名字叫ctr预估,但是它背后算法是线性的逻辑回归. 现在的推荐系统除了可能会用关联规则之外,现在还有一些所谓的更新的协同过滤的算法.
- Stream接口
数据读写可以看作是事件模式(Event)的特例,不断发送的数据块好比一个个的事件.读数据是read事件,写数据是write事件,而数据块是事件附带的信息.Node 为这类情况提供了一个特殊接口Stre ...
- 《精通Spring4.X企业应用开发实战》读后感第六章(内部工作机制、BeanDefinition、InstantiationStrategy、BeanWrapper)
- ARC097D Equals
传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...
- p4503&bzoj3555 企鹅QQ
传送门(洛谷) 传送门(bzoj) 题目 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相 ...
- contentType和dataType的区别
contentType: 告诉服务器,我要发什么类型的数据 dataType:告诉服务器,我要想什么类型的数据,如果没有指定,那么会自动推断是返回 XML,还是JSON,还是script,还是Stri ...
- 打开*.gd文件的方法
问题来了: 你可能会收到*.gd的公文,那么一般的阅读器都打不开…… 解决方法: 使用书生阅读器(三合一版)来打开 地址:http://www.du8.com/download/index.html