[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 ...
随机推荐
- Pytest自动化测试 - allure报告进阶
Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持. 1.严重性 如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个 ...
- 思维导图学《JVM 虚拟机规范》
目录 工具 虚拟机实现 class 文件结构 字节码指令 其他 虚拟机结构 公众号 coding 笔记.点滴记录,以后的文章也会同步到公众号(Coding Insight)中,希望大家关注_ 公众号 ...
- go语言小知识点
fmt包相关 fmt包主要是实现了格式化的I/O函数 fmt.Println() 行打印 fmt.Printf() 格式化输出 %d int %s str %t type,类型 %p 内存地址 % ...
- 保姆级别的RabbitMQ教程!包括Java和Golang两种客户端
目录 什么是AMQP 和 JMS? 常见的MQ产品 安装RabbitMQ 启动RabbitMQ 什么是Authentication(认证) 指定RabbitMQ的启动配置文件 如何让guest用户远程 ...
- 第7.13节 案例详解:Python类变量
第7.13节 案例详解:Python类变量 上节介绍了类变量的定义和使用方法,并举例进行了说明.本节将通过一个更完整的例子来说明. 一. 定义函数dirp def dirp(iter): ret ...
- Python中sorted(iterable, *, key=None, reverse=False)函数参数定义中的独立星号(*)的含义
老猿在 <Python中函数的参数带星号是什么意思?>中介绍了Python函数中参数带星号的含义,而在实际使用和Python的标准文档中,会看到某写函数(如sorted(iterable, ...
- PyQt学习随笔:Model/View架构中的Delegate(委托)
不同于MVC模式,Model/View设计并不包含用于处理与用户交互的完全独立的部件, 没有将用户交互部分完全分离.一般地,视图负责把模型数据显示给用户,以及处理用户的输入.但是,对于某些特殊要求(比 ...
- 缩减项目代码中的大面积if策略
参考设计模式 - 策略模式我们可以优化if-else代码段,而在Spring(Boot)中,借助ApplicationContext扫描,可以使代码更加干净. 话不多说,亮代码: 首先按照策略模式的写 ...
- Javascrip之BOM
重点内容 理解windows对象-BOM核心 控制窗口.框架.弹出窗口 利用location对象中的页面信息 利用navigator对象了解浏览器 BOM:浏览器对象模型[Browner Object ...
- Hbase的基本原理(与HIVE的区别、数据结构模型、拓扑结构、水平分区原理、场景)
重点:HBase的基本数据模型.拓扑结构.部署配置方法,并介绍通过命令行和编程方式使用HBase的基本方法. HBase:一种列存储模式与键值对相结合的NoSQL软件,但更多的是使用列存储模式,底层的 ...