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 ...
随机推荐
- 一次spark卡顿分析
在104上面执行,经常会发生卡到了如下一句话: storage.BlockManagerInfo: Added broadcast_8_piece0 当再次卡顿的时候,我直接退出,然后通过yarn看后 ...
- ORACLE数据库增加表空间大小或给表空间增加数据文件
转载 2017年11月24日 11:00:28 ----查询表空间使用情况--- SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GRO ...
- 【转】 Pro Android学习笔记(五八):Preferences(2):CheckBoxPreference
目录(?)[-] CheckBox Preference xml文件 设备的存贮文件 复合preference 在ListPreference的例子中显示的是单选,如果是多选,可采用CheckBoxP ...
- css3 tranform perspective属性
perspective 属性用于规定观察点距离元素的距离, 1 观察点距离元素越近,元素变形就越大,灭点距离越近. 2 观察点距离元素越远,元素变形越小,灭点距离也就越远. 比如设置perspecti ...
- AD9 如何画4层pcb板
新建的PCB文件默认的是2层板,教你怎么设置4层甚至更多层板. 在工具栏点击Design-->Layer Stack Manager.进入之后显示的是两层板,添加为4层板,一般是先点top la ...
- Ubuntu14.04如何用root账号登陆系统
在虚拟机VMWARE中安装完Ubuntu后,只能用新建的普通用户登陆,很不方便做实验:那如何用root用户登陆账号呢? (1)用普通账号登陆,打开终端terminal: (2)在terminal的输入 ...
- CentOS 配置RDP
XRDP服务器 CentOS安装XRDP实现远程桌面访问: 由于安装的是远程桌面,因此需要安装桌面显示服务:# yum install vnc-server 下面开始配置XRDP服务 l 配置环境: ...
- IOS+openCV在Xcode的入门开发
昨天折腾了一天,终于搞定了openCV+IOS在Xcode下的环境并且实现一个基于霍夫算法的圆形识别程序.废话不多说,下面就是具体的折腾流程: ---------------------------- ...
- 核PCA与PCA的精髓和核函数的映射实质
1.PCA简介 遭遇维度危机的时候,进行特征选择有两种方法,即特征选择和特征抽取.特征选择即经过某种法则直接扔掉某些特征,特征抽取即利用映射的方法,将高维度的样本映射至低维度.PCA(或者K-L变换) ...
- 关于Android阻塞的解决方法
首先新建一个线程,然后有两种方法 1.POST方法(直接,但是可读性差) 2.使用AnycTask类,可读性好,而且是POST方法的封装