Divide Two Integers leetcode java
题目:
Divide two integers without using multiplication, division and mod operator.
题解:
这道题我自己没想出来。。。乘除取模都不让用。。那只有加减了。。。我参考的http://blog.csdn.net/perfect8886/article/details/23040143
代码如下:
1 public int divide(int dividend, int divisor) {
2 if (dividend == 0 || divisor == 0) {
3 return 0;
4 }
5 boolean isNeg = (dividend > 0 && divisor < 0)
6 || (dividend < 0 && divisor > 0);
7 long a = Math.abs((long) dividend);
8 long b = Math.abs((long) divisor);
9 if (b > a) {
return 0;
}
long sum = 0;
long pow = 0;
int result = 0;
while (a >= b) {
pow = 1;
sum = b;
while (sum + sum <= a) {
sum += sum;
pow += pow;
}
a -= sum;
result += pow;
}
return isNeg ? -result : result;
}
Reference:
http://blog.csdn.net/perfect8886/article/details/23040143
Divide Two Integers leetcode java的更多相关文章
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- Divide Two Integers —— LeetCode
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
随机推荐
- MySQL Insert语句单个批次数量过多导致的CPU性能问题分析
[问题] 最近有台服务器比较频繁的CPU报警,表现的特征有CPU sys占比偏高,大量慢查询,大量并发线程堆积.后面开发对insert的相关业务限流后,服务器性能恢复正常. [异常期间线程处理情况] ...
- luogu P3592 [POI2015]MYJ
题目链接 luogu P3592 [POI2015]MYJ 题解 区间dp 设f[l][r][k]表示区间l到r内最小值>=k的最大收益 枚举为k的位置p,那么包含p的区间答案全部是k 设h[i ...
- [BZOJ4668]冷战(并查集)
比较自然的思路是,由于需要记录连通块合并时的信息,所以需要建出Kruskal重构树. 需要用LCT维护,支持加点和在线LCA操作. 不妨考虑在并查集合并的同时记录信息,pre[x]表示x与它的父亲相连 ...
- [BZOJ4487][JSOI2015]染色问题(容斥)
一开始写了7个DP方程,然后意识到这种DP应该都会有一个通式. 三个条件:有色行数为n,有色列数为m,颜色数p,三维容斥原理仍然成立. 于是就是求:$\sum_{i=0}^{n}\sum_{j=0}^ ...
- 【BZOJ】2760: [JLOI2011]小A的烦恼【字符串模拟】
2760: [JLOI2011]小A的烦恼 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 406 Solved: 258[Submit][Statu ...
- word-ladder总结
title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...
- Codeforces Round #359 (Div. 2) B. Little Robber Girl's Zoo 水题
B. Little Robber Girl's Zoo 题目连接: http://www.codeforces.com/contest/686/problem/B Description Little ...
- python 编程语言基础技术框架
python标识符身份 id方法查看唯一标示符,内存地址 >>> a = "str" >>> b = 2 >>> id(a) ...
- OpenVPN分配静态IP以及同一网段内IP个数(64个)
说明:简单的来说,同一网段内可用的IP数量只有64个:(不一定正确)最直接的解释就是每个客户端占用两个IP,因为根据IP掩码位/30得知可用的IP就是两个.对于为什么只有64个,下面是官方的解释. 解 ...
- QN-H618 遥控器复制再生仪(拷贝机)
针对现在市场上日益更新的遥控器种类,本公司经过长时间的研究,推出新一代拷贝机,本产品有以下特点: 1. 众多车库门遥控分析信息被集成在一台机器内,只要一种遥控器,就可以复制众多品牌的车库遥控.免去积压 ...