[leetcode]29. Divide Two Integers不用除法实现除法
思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大。
递归实现
剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将数转成long类型),特殊情况(0除数和商太大)
public int divide(int dividend, int divisor) {
//判断结果的正负
int flag = (dividend<0 != divisor<0)?-1:1;
long lend = Math.abs((long)dividend);
long lor = Math.abs((long)divisor);
//特殊情况
if (divisor==0) return Integer.MAX_VALUE;
long res =ld(lend,lor);
if (res>Integer.MAX_VALUE)
return (flag>0)?Integer.MAX_VALUE:Integer.MIN_VALUE;
else return (int)res*flag;
}
public long ld(long lend,long lor)
{
if (lend<lor) return 0;
long sum = lor;
long m = 1;
//尝试将被除数分成两部分,其中一部分是小于被除数的m倍除数,需要不断尝试
while (sum*2<lend)
{
sum+=sum;
m+=m;
}
//将剩下的部分再分
return m+ld(lend-sum,lor);
}
[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(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [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, 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 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 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 ...
随机推荐
- 20191017_datatable.select() 数据源中没有dataRow
filterStr =" 记录时间 >= '2019/10/17 00:00:00' and 记录时间 <='2019/10/20 23:59:59' " 代码: dg ...
- day2(APlview+Serializers使用)
1.APIview使用 ModelVIewSet 是对 APIView 封装 ModelSerializer是对Serializeer 1.1 在user/urls.py中添加路由 urlpatte ...
- 部署完的Django项目升级为HTTPS
1.阿里云上申请免费ssl证书--->提交各种资料--->等待审核--->下载证书. 2.远程连接阿里云服务器,将下载下来的证书内容复制到Nginx安装目录下的cert目录(需要新建 ...
- 从Paxos到Zookeeper 分布式一致性原理与实践读书心得
一 本书作者介绍 此书名为从Paxos到ZooKeeper分布式一致性原理与实践,作者倪超,阿里巴巴集团高级研发工程师,国家认证系统分析师,毕业于杭州电子科技大学计算机系.2010年加入阿里巴巴中间件 ...
- PyQt(Python+Qt)学习随笔:QTreeWidgetItem项是否首列跨所有列展示属性isFirstColumnSpanned
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeWidget树型部件的QTreeWidgetItem项方法isFirstColumnSpa ...
- Python中使用“模块名.__all__”查看模块建议导出的属性
在<第10.5节 使用__all__定义Python模块导入白名单>中,老猿介绍了在自定义模块中使用定义__all__属性来提供模块对外可见的白名单,使用该机制除了可以定义访问的白名单外, ...
- c++如何按照map的value进行排序?
static bool cmp(pair<char, int> a , pair<char,int> b) { return a.second>b.second; //按 ...
- 事后Postmortem会议
会议图片 一.设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我的软件是要建立一个失物招领网站,是一个为校园里的失误招领工作提供便利的平台.我们对 ...
- 【Alpha冲刺阶段】Day 7
[Alpha冲刺阶段]Scrum Meeting Daily7 1.会议简述 会议开展时间 2020/5/28 8:00-8:30 PM 会议基本内容摘要 讨论合并测试问题 参与讨论人员 项目全体 ...
- 动态规划之经典数学期望和概率DP
起因:在一场训练赛上.有这么一题没做出来. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6829 题目大意:有三个人,他们分别有\(X,Y,Z\)块钱 ...