LeetCode Divide Two Integers

(LeetCode) Divide Two Integers (Java)思路讲解及实现 本文将详细介绍Divide Two Integers 的解题思路及Java实现。 题目如下: Divide two integers without using multiplication, division and mod operator. (在不使用乘法,除法和取余操作的情况下实现整数的除法运算) If it is overflow, return MAX_INT.(整型 阅读详情

原题链接在这里:https://leetcode.com/problems/divide-two-integers/

思路: 从dividend里一个一个减divisor一直到余下的数小于divisor停止,算算一共减了多少个divisor,就是答案。

但如此会超时,所以每次要把divisor乘2^k,再减。表示公式就是dividend = divisor * (2^a+2^b+2^c);

e.g 28/3 => 28/3 = (2^3+2^0), answer = 2^3+2^0=9.

Note:  1. 这道题最火大的是overflow,不但要考虑Math.abs()时的溢出,因为Integer.MIN_VALUE 的绝对值 比 Integer.MAX_VALUE 的绝对值大一。

2. 还要考虑一种特殊的corner case: dividend = Integer.MIN_VALUE, divisor = -1, 此时res = Integer.MAX_VALUE +1,所以cast时会出错,要特殊处理。

3. 当以2的幂次方增长时,就用如下方式来写:

while(){
    sum+=sum;
    pow+=pow;
}


AC Java:

public class Solution {
    public int divide(int dividend, int divisor) {
        if(divisor == 0){
            return Integer.MAX_VALUE;
        }
        
        long res = 0;
        boolean isNeg = (dividend>0&&divisor<0)||(dividend<0&&divisor>0);
        long a = Math.abs((long)dividend);
        long b = Math.abs((long)divisor);
        
        long sum;
        long pow;
        while(a>=b){
            sum = b;
            pow = 1;
            while(sum+sum<=a){
                sum+=sum;
                pow+=pow;
            }
            a-=sum;
            res+=pow;
        }
        res = (isNeg) ? (-res):res;

        if(res > Integer.MAX_VALUE){
            return Integer.MAX_VALUE;
        }
        return (int)res;
    }
}


Figure 03人形机器人与Helix端到端控制系统:具身智能突破深度解析 2026年5月,Figure AI的Figure 03人形机器人在一场震撼业界的直播中完成了长达200小时的连续全自动作业,分拣近25万个包裹,零故障。这一里程碑式的成就标志着人形机器人正式从「实验室展示」迈入「规模化商用」阶段。本文深入剖析Figure 03的核心技术——Helix端到端神经网络控制系统,包括System 0/1/2三层架构、视觉运动策略、全身协同控制等关键技术,并提供完整的Python/Go代码示例,帮助开发者理解具身智能的核心原理与实现路径。关键词。 阅读详情

相关推荐

记一次使用STM32F042K6芯片内部HSI48M时钟导致CAN总线报错问题

深入去解决某个问题时,也会加深你对其它部分的了解和思考,比如这里让我又重新了解和CAN的采样点和CAN报错机制。

ApexYuan 860

图解LeetCode:Divide Two Integers

文章目录我的思路算法优化代码实现总结 这是LeetCode的第29号题目,Divide Two Integers,题目要求计算两个数相除是多少,最后返回整数就可以。 比如给定10和4,那10/4是2;给定7和-3,那么7/(-3)应该返回2,题目要求不能用乘法、除法和取余操作。如果在面试时遇到这个题目该怎么求解呢?   我的思路 首先我们可以将除数和被除数都转为正数以简化操作,而对于除法...

码农的荒岛求生 398

稚晖君Dummy机械臂CAN总线控制详解:从代码到实战(附常见问题排查)

本文深入解析了稚晖君Dummy机械臂的CAN总线控制技术,从协议设计到代码实现,详细介绍了CAN总线在机械臂控制中的核心价值、通信协议栈设计、运动控制指令实现及常见问题排查方法。通过实战案例和代码示例,帮助开发者快速掌握机械臂CAN总线控制的关键技术。

uuu88的博客 294

Leetcode 29 —— Divide Two Integers

29. Divide Two Integers My Submissions Question Total Accepted: 62780 Total Submissions: 403300 Difficulty: Medium Divide two integers without using multiplication, division and mo

decode 1042

实验吧 损坏的U盘镜像

        从网站上可以下载一个CTF.hdd的文件,使用winHex打开,可以知道这是一个exFAT格式的文件。        用binwalk检查一下,发现文件中包含两个zip文件,使用foremost进行提取。一个压缩包需要密码解压缩,另一个解压后发现Password.txt文件。说明另一个压缩包的密码与checksum有关。        通过查看exFAT文件系统格式可以得知如何计算...

xingyyn78的博客 3512

华为OD机试 - 比赛的冠亚季军(Python/JS/C/C++ 双机位C卷 100分)

有个运动员,他们的id为0到N-1,他们的实力由一组整数表示。他们之间进行比赛,需要决出冠亚军。比赛的规则是0号和1号比赛,2号和3号比赛,以此类推,每一轮,相邻的运动员进行比赛,获N (3\leq N< 10000)胜的进入下一轮,实力值大的获胜,实力值相等的情况,id小的情况下获胜,轮空的直接进入下一轮。(0 <= 实力值 <= 10000000000)。输入一行N个数字代表N的运动员的实力值。输出冠亚季军的id,用空格隔开。

学Java,找哪吒 1587

Divide Two Integers -- LeetCode

原题链接: http://oj.leetcode.com/problems/divide-two-integers/  这道题属于数值处理的题目,对于整数处理的问题,在Reverse Integer中我有提到过,比较重要的注意点在于符号和处理越界的问题。对于这道题目,因为不能用乘除法和取余运算,我们只能使用位运算和加减法。比较直接的方法是用被除数一直减去除数,直到为0。这种方法的迭代次数是结

Code Ganker 3万+

【数据分享】2012-2022年全国及各城市POI(兴趣点)数据(超20G)

POI(一般作为Point of Interest的缩写,也有Point of Information的说法),通常称作兴趣点,泛指互联网电子地图中的点类数据,基本包含名称、地址、坐标、类别四个属性;随着互联网电子地图服务与LBS应用的普及,POI无论从概念范畴,还是从信息纵深都有了长足发展,互联网各个风口或火山口都和POI有一定关系,如O2O、电商、社交、互联网金融、共享经济等。

J_Giser的博客 2730

[leetcode javascript解题]Divide Two Integers

leetcode第29题,“Divide Two Integers” 的描述如下: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 题目就是要大家不使用乘除法和模运算来实现除法。同时要考虑边界条件。 这道题在lee

zzxboy1的博客 969

Python爬虫技术:使用Playwright和Twitter API v2高效获取Twitter/X数据完全教程

遵守规则:始终遵循Twitter的使用条款和速率限制错误处理:实现完善的错误处理和重试机制数据质量:进行数据清洗和验证性能优化:使用异步处理和合理的延迟设置可维护性:模块化代码,便于维护和扩展。

2201_76125261的博客 671

从DDPM到DDIM:深入解读《Denoising Diffusion Implicit Models》

DDIM发表在ICRL2021上,是DDPM重要的改进之一,能显著提高DDPM的样本质量、减少采样时间,并且能显式控制插值,已经被广泛应用到现在的Diffusion Models上。这篇博客和大家一起详细解读一下DDIM,认识这一伟大的模型。.........

沉迷单车的追风少年 1万+

LeetCode:29. Divide Two Integers

LeetCode:29. Divide Two Integers Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by d...

Wisimer 182

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

leetcode学习笔记29 Divide Two Integers

leetcode学习笔记29问题方法1方法2 问题 leetcode Two Sum 方法1 时间复杂度O(n2)O(n^{2})O(n2). 空间复杂度O(1)O(1)O(1). 方法2 时间复杂度O(n)O(n)O(n). 空间复杂度O(n)O(n)O(n).

kuma的博客 281

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

leetcode 29 - Divide Two Integers

leetcode题目汇集 - Divide Two Integers leetcode 29 - Divide Two Integers 题目要求 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思

知足常乐,知行合一 275

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

leetcode-【中等题】Divide Two Integers

题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT 链接 https://leetcode.com/problems/divide-two-integers/ 答案 1、int的最大值MAX_INT为p...

aoyuan4706的博客 141

LeetCode的一个题目 Divide Two Integers

昨天开始刷LeetCode,刚开始做的几个题都没有问题,思路都很流畅,知道遇到了Divide Two Integers这个题目。 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 题目的描述很简单,就是在不使

Seek42的专栏 495

[leetcode] 29. Divide Two Integers

Description 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 di...

Learning from the mistakes 307

leetcode29. Divide Two Integers

leetcode29. Divide Two Integers 思路:辗除法可得到商,二进制辗除法和十进制是类似的,只是十进制的时候是每次进位是10,而二进制每次进位是2,且十进制进位方式是*10,二进制是左移一位。 x = ai*10^i+… x = ai*2^i+…. 一个道理引申出题:只需用乘法,不许用除法,获取商。则可用类似原理解题

慕鹞 181

leetcode-29. Divide Two Integers

leetcode-29. Divide Two Integers题目: Divide two integers without using multiplication, division and mod operator. 第二遍做的时候还是没想起来。。其实就是二分法。但是跟一般的二分有点不一样。里面要嵌套一个while保证除数以2的幂的速度增长。这样先找结果的最高位,在找次高位以此类推。直到

千念飞羽的专栏 313

LeetCode 29. Divide Two Integers(二分)

题目来源:https://leetcode.com/problems/divide-two-integers/ 问题描述 29.Divide Two Integers Medium Given two integersdividendanddivisor, divide two integers without using multiplication, division and ...

da_kao_la的博客 395

Leetcode1-100: 29. Divide Two Integers

Leetcode1-100: 29. Divide Two Integers问题描述解题思路代码实现    错误实现    正确实现分析 问题描述 Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient afte

Y123iwantit的博客 233

leetcode之Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. 分析:

fangjian1204的专栏 1254

Leetcode: Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 用减法:Time Limit Exceeded int divide(int dividend, int divisor) { // Note: The Solution object is instantiate

ZkvIA的博客 8703

LeetCode 29. Divide Two Integers

LeetCode 29. Divide Two Integers Solution1: 参考网址:[1]https://blog.youkuaiyun.com/qq_31617121/article/details/80458587 [2]http://www.cnblogs.com/grandyang/p/4431949.html 相当于复杂度优化到logN. 如 100 3 普通方法是 3...

Allenlzcoder的博客 314
上一篇: LeetCode Add Binary
下一篇: LeetCode Add Digits
Dylan_Java_NYC
博客等级 码龄11年 1粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值