【LeetCode每天一题】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.
Example 1: Input: dividend = 10, divisor = 3 Output: 3
Example 2: Input: dividend = 7, divisor = -3 Output: -2
Note:
- Both dividend and divisor will be 32-bit signed integers.
- The divisor will never be 0.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
思路
在题中明确了不能使用乘法和除法以及模运算,所以我们只能通过其他办法来进行解决。除法解决的问题就是看被除数里面有多少个除数的问题,最简单的方法就是我们可以使用减法,对被除数减去除数,然后计数加一。但是这种办法当从被除数较大时时间复杂度较高,因此我们可以对其进行改进,对被除数从除数的倍数开始减去,然后每次相减完毕之后对对除数进行加倍。这样当被除数比较大时,也能很快的相减完毕。
解决代码
class Solution(object):
def divide(self, dividend, divisor):
positive = (dividend < 0) is (divisor < 0) # 被除数和除数为负数的情况进行考虑。
dividend, divisor = abs(dividend), abs(divisor) # 对其取绝对值
res = 0 # 结果
while dividend >= divisor: # 当被除数小于当前除数时,说明已经不能被整除了。
tem, i = divisor, 1 # 存储倍数和除数
while dividend >= tem: # 当被除数小于当前倍数除数时,终止循环
dividend -= tem # 被除数减去除数
res += i # 结果加上相应的倍数
i <<= 1 # 除数的倍数
tem <<= 1 # 除数翻倍
if not positive: # 判断是否有负数
res = -res
return min(max(-2147483648, res), 2147483647) # 超出范围判断
【LeetCode每天一题】Divide Two Integers(两整数相除)的更多相关文章
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 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 ...
- leetcode第28题--Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- lintcode 中等题:Divide Two Integers 两个数的除法
题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题 15%的通过率 ...
随机推荐
- day_5.28 py网络编程
端口 socket简介: socket为一个类 s接收的是返回的对象引用 2018-5-28 15:52:47 开始进行网络编程 udp 套接字 encode() 编码 decode() 解码 ' ...
- redis(三)--用Redis作为Mysql数据库的缓存
把MySQL结果集缓存到Redis的字符串或哈希结构中以后,我们面临一个新的问题,即如何为这些字符串或哈希命名,也就是如何确定它们的键.因为这些数据结构所对应的行都属于某个结果集,假如可以找到一种唯一 ...
- 基于jdk1.8的HashMap源码学习笔记
作为一种最为常用的容器,同时也是效率比较高的容器,HashMap当之无愧.所以自己这次jdk源码学习,就从HashMap开始吧,当然水平有限,有不正确的地方,欢迎指正,促进共同学习进步,就是喜欢程序员 ...
- HTML滚动文字代码 marquee标签
看到一个HTML滚动文字代码 marquee标签的内容,非常全面,而且觉得有点意思,可以让大家为自己博客或者网站设置一个漂亮的滚动文字. 以下是原文: <marquee style=" ...
- 项目()已配置为使用IIS Web服务器,但此计算机上...
问题:x 就是要将一个项目,配置在IIS上,以前没遇上过这种开发模式啊... 解决方案: 0.如果配置为不使用IIS Web服务器,将Project.csproj中的" <UseIIS ...
- python3读取excel数据
import xlrd worksheet = xlrd.open_workbook('XXXX.xlsx') #打开excel文件 sheet_names= worksheet.sheet_na ...
- Java 输入/输出——File类
File类是java.io包下代表与平台无关的文件和目录,也就是说,如果希望在程序中操作文件和目录,都可以通过File类来完成.值得指出的是,不管是文件还是目录都是使用File来操作的,File能新建 ...
- iOS-静态库,动态库,framework浅析(三)
创建framework静态库 第一步,新建项目 新建项目.png 第二步,删除系统默认创建的[FMDB.h]和[FMDB.m]文件,导入需要打包的源文件. 导入源码后的工程.png 第三步,修改项目配 ...
- C#查找字符串位置
int i=zifuchuan.IndexOf(","); int n=(zifuchuan.SubString(i+1)).IndexOf(","); int ...
- LeetCode 283 Move Zeroes 解题报告
题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...