LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
package leetcode_50; /***
*
* @author pengfei_zheng
* 不使用乘法、除法、求模实现除法运算
*/
public class Solution29 {
public int divide(int dividend, int divisor) {
long result = divideLong(dividend, divisor);
return result > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)result;
} // It's easy to handle edge cases when
// operate with long numbers rather than int
public long divideLong(long dividend, long divisor) { // Remember the sign
boolean negative = dividend < 0 != divisor < 0; // Make dividend and divisor unsign
if (dividend < 0) dividend = -dividend;
if (divisor < 0) divisor = -divisor; // Return if nothing to divide
if (dividend < divisor) return 0; // Sum divisor 2, 4, 8, 16, 32 .... times
long sum = divisor;
long divide = 1;
while ((sum+sum) <= dividend) {
sum += sum;
divide += divide;
} // Make a recursive call for (devided-sum) and add it to the result
return negative ? -(divide + divideLong((dividend-sum), divisor)) :
(divide + divideLong((dividend-sum), divisor));
}
}
LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)的更多相关文章
- [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] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [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, division ...
- [leetcode]29. Divide Two Integers不用除法实现除法
思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
随机推荐
- utf8 ucs4
这个问题不好回答,首先UTF-8编码只不过是一种Unicode的转换,兼容ASCII.所以,UTF-8编码支持的最大字符编码应该是Unicode支持的最大字符编码. 理论上,UTF-8编码可以支持最大 ...
- Scala学习笔记——入门
0.在 scala> 下运行Scala程序 首先cd到.scala文件所在的目录下 scalac这个scala文件,然后import package的名字.object的名字 然后就能使用 ob ...
- 配置Server.xml
Service下面的Connector, Engine, Executor. 组件的目录结构,配置文件,配置节点.
- RTC教程
Tutorial: Get started with Rational Team Concert Getting Started with Jazz Source Control RTC入门教程及冲突 ...
- 决策树之Cart算法一
Contents 1. CART算法的认识 2. CART算法的原理 3. CART算法的实现 1. CART算法的认识 Classification And Regression ...
- jquery.form 和MVC4做无刷新上传DEMO
jquery.form 和MVC4做无刷新上传DEMO HTML: <script src="~/Scripts/jquery-1.10.2.min.js"></ ...
- 五步整理你的css文件
鉴于实在无法忍受那种写一句就换一行的css写法,有个项目中的一个css文件竟然高达6000多行,看着实在蛋疼,无实今天下定决心整理一下,在DW里可以用正则很好的进行替换,步骤如下: 一:\r => ...
- symfony获取POST数据
$request = $this->getRequest(); $messageid = $this->strip_tags($request->get('messageid')); ...
- python中是否有单独的字符类型,通过下标的方式表示字符串中的字符
说明: 在python中,没有单独的字符类型,一个字符呢就是一个大小为1的字符串. 并且可以通过下标的方式,表示字符串中的字符. 操作过程: 1.通过[ ]的方式表示字符串中的第几个字符 >&g ...
- 解决win10休眠后无法唤醒
在控制面板-电源选项-编辑计划设置-高级电源设置中把"睡眠"的选项中休眠调整为从不,"电源按键和盖子"选项中也都设为睡眠,这样使得无论你是使用电池还是电源,系统 ...