[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 ...
随机推荐
- P3501 [POI2010]ANT-Antisymmetry
P3501 [POI2010]ANT-Antisymmetry 二分+hash 注意:答案超出int范围 ------------ 先拿一个反对称串来做栗子:010101 我们可以发现 0101(左边 ...
- Android实践项目汇报-改(一)
Google天气客户端NABC Need(需求): 功能性需求分析 天气预报客户端,顾名思义就是为用户提供实时准确的天气信息,方便用户出行生活.根据用户日常需求,软件完成后点开,载入界面,显示查询界 ...
- python判断结构总结
1.判断结构是允许程序针对不同情况执行不同指令序列的控制结构. 2.判断在Python中用if语句实现.简单的判断是用一个简单的if来实现的.两路判断通常使用if-else.多路判断用if-elif- ...
- python2.7+pyqt4 +eric4安装配置
eric4安装与汉化一直没找到合适python的IDE工具,直到遇到了eric4这款开源软件.然而在使用过程中发现输出的中文字符竟然是乱码,修修改改配置总算正常显示了,何不干脆把软件界面也汉化下. 一 ...
- Dubbo学习参考
参考博客: 小宝鸽:https://blog.csdn.net/u013142781/article/details/50387583 https://blog.csdn.net/u013142781 ...
- 2、extract-text-webpack-plugin提取Sass编译的Css
cnpm install css-loader --save-dev //css-loader 是将css打包进js cnpm install style-loader --save-dev ...
- Unity3D学习笔记(十六):Animator新动画
新动画系统: 给模型选择动画类型 普通动画:Generic 人形动画:Humanoid 建立动画控制器 - 在Project右击 - 选择Create-AnimatorContorller 将对应动画 ...
- Tex: The top-level auxiliary file: *.aux I couldn't open style file IEEEtran.bst 解决方法
参考: Bibliography is not printed using Kile on Ubuntu Tex: The top-level auxiliary file: *.aux I coul ...
- UVa 11093 环形跑道(模拟)
https://vjudge.net/problem/UVA-11093 题意:环形跑道上有n个加油站,编号为1~n.第i个加油站可以加油pi加仑,从加油站i开到下一站需要qi加仑汽油.输出最小的起点 ...
- H5本地存储二
众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...