[LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
乘除取模都不让用。。那只有加减
思路解析:
判空,返回0
使用long类型的变量存储division和divisor的绝对值
如果除数小于被除数,返回0
使用加法完成除法,注意保存加了多少倍,使用嵌套循环,外层控制division大于divisor,里层控制倍数增加一倍是否比division要小
最后返回值的时候不要忘记注意正负号
public int divide(int dividend, int divisor) {
if (dividend == 0 || divisor == 0)
return 0;
boolean flag = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
long a = Math.abs((long) dividend);// 注意要转换成long型,防止溢出
long b = Math.abs((long) divisor);
if (a < b)
return 0;
int result = 0;
while (a >= b) {
long tempSum = b;
long divSum = 1;
while (tempSum + tempSum <= a) {
tempSum += tempSum;
divSum += divSum;
}
a -= tempSum;
result += divSum;
}
return flag == true ? -result : result;
}
[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 ☆☆
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, divisio ...
- [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 ...
- 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://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- 01: html常用标签
目录: 1.1 web开发的三把利器介绍 1.2 网页头部head标签中几个常用标签 1.3 html常用标签归类 1.4 input系列标签 1.5 HTML其他标签 1.1 web开发的三把利器介 ...
- 基于qml创建最简单的图像处理程序(1)-基于qml创建界面
<基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...
- libcurl HTTP POST请求向服务器发送json数据
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- 完整的Android开发环境Eclipse+ADT+SDK(22.0.1)
现在开始学习Android嵌入式编程,首要的问题就是在Windows中搭建开发环境,就这个都要摸索很长的时间,总是在版本之间折腾折腾去,而且Google的Android正式差劲得很,经常是连不上,要不 ...
- Farey Sequence (素筛欧拉函数/水)题解
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...
- IIS Logs
日志路径 %SystemDrive%\inetpub\logs\LogFiles https://stackify.com/where-are-iis-log-files-located/ Where ...
- 谈谈java中的final关键字
知识点:final(最终的)关键字修饰类.方法.属性 1.final修饰类:那么这个就无法被继承,如String类.StringBuffer类.System类 2.final修饰方法:被修饰的方法不能 ...
- 阿里云Linux服务器初探
阿里云Linux服务器初探 阿里云Linux服务器初探 因为钱包的关系,本人买了一个660元2年的1核1GB的小服务器(centos是Linux的发行版),在当初是用2核4GB(内存)的时候使用的是w ...
- python里的apply,applymap和map的区别
apply,applymap和map的应用总结: apply 用在dataframe上,用于对row或者column进行计算: applymap 用于dataframe上,是元素级别的操作: map ...
- Window下的git配置文件在哪里【图文】
来源:https://jingyan.baidu.com/article/870c6fc3589f22b03fe4be95.html 第一次使用码云建仓库总是提示各种错误,遂,从头在学一遍git,改篇 ...