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 ...
随机推荐
- chromedriver与chrome版本对应表,firefox、geckodriver
一. chromedriver与chrome对应表(记得就会更新): chromedriver版本 支持的Chrome版本 v2.36 v64-66 v2.35 v62-64 v2.34 v61-6 ...
- 张忠谋:摩尔定律将死,物联网是“Next Big Thing”
2014台湾半导体产业协会(TSIA)年会于27日登场,台积电董事长暨TSIA名誉理事长张忠谋以"Next Big Thing"为题发表演说.张忠谋表示,他认为摩尔定律 ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
- Bunch 转换为 HDF5 文件:高效存储 Cifar 等数据集
关于如何将数据集封装为 Bunch 可参考 关于 『AI 专属数据库的定制』的改进. PyTables 是 Python 与 HDF5 数据库/文件标准的结合.它专门为优化 I/O 操作的性能.最大限 ...
- egrep 第几列开始
第6位开始 egrep -a '^.{5}(0|5)' ${CACSDATA}/outcds.ur5.ca2.txt | sort > ${CACSDATA}/outcds.a.ur5.txt
- 删除或修改eclipse中svn的账号密码
由于eclipse没有自带的管理svn账号的功能,我也没有找到相关的插件,要是有朋友知道的话也可以跟我说下哦!以下是关于自己手动去删除eclipse 软件的 svn账号,以便切换项目的时候去更换svn ...
- 1023 Have Fun with Numbers (20)(20 point(s))
problem Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 t ...
- LOJ P3960 列队 树状数组 vector
https://www.luogu.org/problemnew/show/P3960 树状数组预处理之后直接搞就可以了,也不是很好解释,反正就是一个模拟过程的暴力用树状数组维护,还挺巧妙的. 我为什 ...
- 【漏洞预警】Intel爆CPU设计问题,导致win和Linux内核重设计(附测试poc)
目前研究人员正抓紧检查 Linux 内核的安全问题,与此同时,微软也预计将在本月补丁日公开介绍 Windows 操作系统的相关变更. 而 Linux 和 Windows 系统的这些更新势必会对 Int ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...