思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大。

递归实现

剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将数转成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不用除法实现除法的更多相关文章

  1. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  2. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  3. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  4. [LeetCode] 29. Divide Two Integers ☆☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  5. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  9. LeetCode: 29. Divide Two Integers (Medium)

    1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...

随机推荐

  1. web服务器专题:tomcat基础及模块

    Web服务器专题:Tomcat(一)基础架构 针对java系的经典服务器,打算系统的整理一下Tomcat的机制和一些原理,以此记录. 插一则题外话,关于tomat这个名字的由来:Tomcat 名称的由 ...

  2. 使用SpringSecurity Oauth2.0实现自定义鉴权中心

    Oauth2.0是什么不在赘述,本文主要介绍如何使用SpringSecurity Oauth2.0实现自定义的用户校验 1.鉴权中心服务 首先,列举一下我们需要用到的依赖,本文采用的是数据库保存用户信 ...

  3. win10拔下电源会黑一下屏

  4. day6(celery配置与基本使用)

    1.celery配置与基本使用 1.1 安装celery pip install celery @ https://github.com/celery/celery/tarball/master 1. ...

  5. (七)if/else就是goto

    一.CPU如何执行指令 CPU上有数以亿计的晶体管组层的复杂电路,我们先不用管具体电路如何实现:逻辑上我们可以认为CPU由许多寄存器组成,而这些寄存器又由许多锁存器和触发器组成,N个锁存器或触发器就可 ...

  6. CSS聚光灯文字(无图片)

    Welcome to my admin site! h1 { font-size: 70px; color: rgba(255, 255, 255, 1); padding: 0; margin: 0 ...

  7. PyQt(Python+Qt)学习随笔:纯文本编辑器QPlainTextEdit功能详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.概述 QPlainTextEdit是用于纯文本的一个高级文档编辑器 ...

  8. PyQt(Python+Qt)学习随笔:model/view架构中QTableView视图的标题显示不正常问题

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在进行QTableView展示数据时,使用了QStandardItemModel的model,并在将 ...

  9. 在CTF比赛中,命令中空格被过滤的解决方法

    1.linux {cat,flag.txt} cat${IFS}flag.txt cat$IFS$9flag.txt cat<flag.txt cat<>flag.txt kg=$' ...

  10. ES6新特性之箭头函数与function的区别

    写法不同 // function的写法 function fn(a, b){ return a+b; } // 箭头函数的写法 let foo = (a, b) =>{ return a + b ...