[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 ...
随机推荐
- react高阶组件的一些运用
今天学习了react高阶组件,刚接触react学习起来还是比较困难,和大家分享一下今天学习的知识吧,另外缺少的地方欢迎补充哈哈 高阶组件(Higher Order Components,简称:HOC) ...
- virtualProtect函数
原文链接:https://blog.csdn.net/zacklin/article/details/7478118 结合逆向课件11
- JZOJ2020年8月13日提高组反思
JZOJ2020年8月13日提高组反思 T1 打了3h+,然后自己的小数据都没过 果断选择交对拍的暴力 下次还是注意时间吧 T2 一下三题都没时间打了 看了题目觉得特别烦人(有式子) 再看发现式子类似 ...
- java并发编程实战《五》死锁
一不小心就死锁了,怎么办? 在上一篇文章中,我们用 Account.class 作为互斥锁,来解决银行业务里面的转账问题,虽然这个方案不存在并发问题,但是所有账户的转账操作都是串行的,性能太差. 向现 ...
- PHP代码审计分段讲解(8)
20 十六进制与数字比较 源代码为: <?php error_reporting(0); function noother_says_correct($temp) { $flag = 'flag ...
- CF1147F Zigzag Game & 稳定婚姻问题学习笔记
CF1147F Zigzag Game 这题太神仙了,不得不记录一下. 我网络流做不动了,DS做不动了,DP做不动了,特别自闭.于是博弈论之神(就是随手切3500博弈的那种) \(\color{bla ...
- spark有个节点特别慢,解决办法
除解决数据倾斜问题外,还要开启推测执行,寻找另一个executor执行task,哪个先完成就取哪个结果,再kill掉另一个.
- MVCAdmin项目知识点记录
1.在过滤器中,用ViewBag类似的东西,要((ViewResult)filterContext.Result).ViewBag. 2.Controller中自己定义的非Action方法中(包括构造 ...
- git相关操作
git相关命令 基本操作 git init git add xxx git commit -m "first commit" git tag -a V1.0 -m '我的标签' g ...
- java多线程之消费生产模型-使用synchronized解决虚假唤醒
package com.wenshao.juc; /** * 生产者和消费者案例 * * @author Administrator * */ public class TestProductorAn ...