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 ...
随机推荐
- 关系运算符 逻辑运算符 if 语句 switch语句
1. BOOL类型 BOOL isRightOrNo = YES; isRightOrNo = 56;//可以打印出来,在C语言中,非0即真 printf("%d\n" , isR ...
- Poj 2662,2909 Goldbach's Conjecture (素数判定)
一.Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard ...
- HDOJ1150(最小点集覆盖)
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> ...
- 加载某个页面(A)时实现自动跳转到某个页面(B)
<head> <title></title> <script type="text/javascript"> function fu ...
- 8.ireport 取消自动分页,detail不分页
转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 报表文件属性页面 lgnore pagination 勾选上,就可以取消 ...
- k8s 基础 pod操作
创建hell world pod #vim hello-world-pod.yaml apiVersion: v1 kind: Pod metadata: name: hello-world spec ...
- go 语言 基础 类型(1)
变量 使用关键字 var定义变量,自动初始化为0值.如果提供初始化值,可省略变量类型,由编译器自动推断. 在函数内部可以使用 := 方式定义变量 func main() { x := 123 } 可一 ...
- shell解决DOS攻击生产案例
解决DOS攻击生产案例企业实战题5:请用至少两种方法实现!写一个脚本解决DOS攻击生产案例.提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封 ...
- spring 4.0 JUnit简单的Dao,Service测试
1.AbstractTransactionalJUnit4SpringContextTests 和AbstractJUnit4SpringContextTests.我们在测试用例类要继承两种中的一个. ...
- MySQL在linux下安装
mysql在linux下的安装 安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloa ...