#29 Divide Two Integers

做蛋白组学数据分析,竟然还不会使用DIA-NN软件,走过路过不要错过,进来免费学习!!! 蛋白组学分析工具DIA-NN使用 阅读详情

题目:

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.


题解:

不用乘,除,模操作完成除法运算。只能加减了,但逐个减效率低,肯定超时,想不出来,参考了http://blog.youkuaiyun.com/perfect8886/article/details/23040143。猜测题目中第二行testcase可能溢出的情形是后来增加的,所以该博客发表时没有对溢出加以考虑。


思路:
1.先将被除数和除数转化为long的非负数,注意一定要为long,因为Integer.MIN_VALUE的绝对值超出了Integer的范围。

***2.常理:任何正整数num都可以表示为num=2^a+2^b+2^c+...+2^n,故可以采用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+2^c+...+2^n),(a,b,c,....m互不相等,且最大为31,最小为0)。而商的最大值为Integer.MIN_VALUE的绝对值,商最多有32个2的指数次相加,故时间复杂度为常数。          

3.divisor*2^a用计算机表示为divisor<<a; 注意:若每次只加一个divisor,则面对Integer.MAX_VALUE除以一个很小的常数(eg:1,2,3),会超时。


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



Windows 下 Claude Code Desktop 安装与使用全记录:从零到一的保姆级图文教程 本文主要介绍了Windows 下 Claude Code 安装全记录:从零到一的保姆级图文教程,希望能对学习使用 Claude Code 的同学们有所帮助。 文章目录 1. 背景 2. 安装Claude Code Desktop 3. 配置第三方API 4. 日常使用 阅读详情

相关推荐

直流有刷电机三环PID控制:从硬件配置到软件调试全解析

本文全面解析了直流有刷电机的三环PID控制技术,涵盖从硬件选型(如驱动板、编码器)到STM32软件配置的全过程。详细阐述了位置环、速度环、电流环的串级协同工作原理与调试方法,并提供了位置式PID算法的C语言实现代码及参数整定技巧,旨在帮助开发者实现精准、稳定的电机运动控制。

weixin_29290963的博客 390

Leecode #29 Divide Two Integers

一、 问题描述 Leecode第二十九题,题目为: Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by divisor. ...

serena_t的博客 339

保姆级教程:用Intouch SMC连接S7-200 Smart PLC,Modbus TCP和S7协议一次搞定

本文详细介绍了如何使用Intouch SMC连接S7-200 Smart PLC,涵盖Modbus TCP和S7协议的双协议通信配置。通过环境准备、参数设置、常见问题排查及实战技巧,帮助工程师快速实现工业自动化系统中的稳定通信,提升设备监控效率。

weixin_30499315的博客 434

LeetCode | # 29. Divide Two Integers

29. Divide Two Integers Description 描述:https://leetcode.com/problems/divide-two-integers/description/ Solution: (Java) class Solution { public int divide(int dividend, int divisor) { if (d...

Karmen199x的博客 180

ROS中costmap_converter插件的配置与使用

在学习TEB算法的过程中,作者提供了一个插件用于转换代价地图,原始的代价地图是由栅格地图中单元格组成,用于表示障碍物,但单元格占用的计算资源较大,故采用插件将单元格转换成点,线,多边形表示。 插件激活 rospack plugins --attrib=plugin costmap_converter 配置cfg文件 在导航配置文件下,新建costmap_converter_params.yaml #######################################################

weixin_42621524的博客 5905

【人工智能】-01、人工智能概述

从理论上讲没有当今计算机不能计算求解的问题(B)。A. 是B. 不是C. 尚无定论。

super_mochi1 353

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 divisor. The integer divis...

LawFile的博客 135

Linux下的隐藏技术(文件隐藏、进程隐藏、端口隐藏、权限隐藏、命令隐藏)

这种技巧是关键记录删除,或者我们可以暴力点,比如前150行是用户的正常操作记录,150以后是攻击者操作记录。在Linux中,使用chattr命令来防止root和其他管理用户误删除和修改重要文件及目录,此权限用ls -l是查看不出来的,从而达到隐藏权限的目的。上面的命令会临时禁用历史功能,这意味着在这命令之后你执行的所有操作都不会记录到历史中,然而这个命令之前的所有东西都会原样记录在历史列表中。这种情况下我们怎么办?在shell中执行的命令,不希望被记录在命令行历史中,如何在linux中开启无痕操作模式呢?

墨痕诉清风的博客 751

LeetCode #29 Divide Two Integers

LeetCode #29 Divide Two Integers Question Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution Approach #1 class Solution { func...

weixin_30824277的博客 105

1.嵌入式控制器EC学习,编译环境搭建

工欲善其事,必先利其器。 在学习EC相关知识之前,首先需要完成EC代码编译环境的搭建,需要如下内容: Keil C51(用于EC中C代码的编译器环境) EC源代码,我们使用从网上可以下载到的 ITE_V12.4_Update 版的代码为例进行学习 代码编辑器,这个可以根据自己的习惯选择,VS Code或者Source Insight等 Keil C51下载地址: http://www.downcc.com/soft/307131.html 下载完成后请进行破解 EC源代码 ...

qq_30135687的博客 3918

ICC2:使用analyze_lib_cell_placement检查lib cell的pass rate

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?在place之前需要使analyze_lib_cell_placement命令去检查lib cell在当前的site row/power plan/legalize设置的环境下能够正常place和legalize。下面分享一个脚本去报告pass rate低于2%的lib cell。

m0_61544122的博客 676

[LeetCode] #29 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 本题是求除法,但是要注意int的边界值。时间:14ms,代码如下: class Solution { public: int divide(int div...

weixin_30566111的博客 79

Python语言基本语法元素

Python语言的基本语法元素:包括Python程序的格式框架,语法元素名称,数据类型和语句元素以及基本的输入输出函数。

Python全栈系列 8366

Leetcode #29 Divide Two Integers 整数相除 解题报告

1 解题思想就是两个整数相除,不能使用内置的运算,如果溢出那么久返回最大的整数2 原题Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT. 题目需要拆解成一个基数为2的。请看我代码里面的那个连接今天好忙来不及仔细说了,见谅3 AC解

MebiuW的专栏 721

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【一天一道LeetCode】#29. Divide Two Integers

一天一道LeetCode系列(一)题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (二)解题这题看起来很简单,一开始想到的方法就是从0开始一次累加除数,一直到比被除数大为止,好无悬念,这样做的结果就是超时了

ZeeCoder 724

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

DAY15:leetcode #29 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Subscribe to see which companies asked this question class Solution(objec

zcy19941015的博客 298

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

[ LeetCode ]#29. Divide Two Integers(两数相除 C++ & Python)

题目:29. Divide Two Integers Given two integersdividendanddivisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividingdividendbydiviso...

xn's blog 562

Divide Two Integers_Leetcode_#29

1 题目 Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.2 解法public class Solution { public int divide(int dividend, int divisor) {

LittleBob180的专栏 301

【LeetCode】#29两数相除(Divide Two Integers)

【LeetCode】#29两数相除(Divide Two Integers) 题目描述 给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。 返回被除数 dividend 除以除数 divisor 得到的商。 示例 示例 1: 输入: dividend = 10, divisor = 3 输出: 3 示例 2: 输入: dividen...

weixin_43858604的博客 232

29. Divide Two Integers

1.问题描述 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 2.解题思路 采用二分法,每次让除数以2倍的形式递增,只要递增结果小于被除数,则继续递增,如果递增时超过了,则记住递增前的数,然后用被除数减去

HuZhenXing 472

[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 308

Leetcode 29. Divide Two Integers 两数相除

Leetcode 29. Divide Two Integers 两数相除 标签: Leetcode 题目地址: https://leetcode-cn.com/problems/divide-two-integers/ 题目描述 给定两个整数,被除数&nbsp;dividend&nbsp;和除数&nbsp;divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。 返回被除数&nbs...

CoderWangSon 309

leetcode29_Divide Two Integers

一.问题描述 Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 实现两个int的除法,代码中不能使用乘、除和取余操作,溢出时返回max_int。 二.代码编写     最基

越努力越幸运 389

Leetcode29 Divide Two Integers

Leetcode29 Divide Two Integers题意理解解题思路代码 题意理解 题目全文如下: Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after di...

wxwjyq的博客 216

【每日一题】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 divisor. The integer division should truncate toward zero, which means losing its frac

Viking的博客 938
上一篇: #17 Letter Combinations of a Phone Number有问题
下一篇: #189 Rotate Array
private_void
博客等级 码龄15年 3粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值