【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%的通过率 ...
随机推荐
- css---点击显示和隐藏
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js中parseInt和Number
昨天在项目中遇到一个问题,有关字符串准换成数字的问题,具体如下: 页面中<input type="number" id="bankNum" ng-mode ...
- HTTP 响应状态码
MDN https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status section 10 of RFC 2616 https://tools.ie ...
- python爬虫之网页解析
CSS Selector 与Xpath path = ‘D:\\Postgraduate\\Python\\python_projects\\Python视频 分布式 爬虫Scrapy入门到精通\\第 ...
- ABP之事件总线(1)
什么是事件总线呢?官方的文档说,它是一个单例对象,由其他的类共同拥有,可以用来触发和处理事件.这个东西确实比较陌生,为什么要使用事件总线,或者说事件总线的优势是什么???首先我们可以明确的是,事件总线 ...
- python 利用tkinter模块设计出window窗口(搞笑版)
代码如下 from tkinter import * import tkinter from tkinter import messagebox #定义了一个函数,当关闭window窗口时将会弹出一个 ...
- 鸡头兔头共20,脚56,鸡兔各有多少?算法实 php现版
//$x 鸡头 //$y 兔头 for ($x = 0; $x <= 20; $x++) { for ($y = 0; $y <= 20; $y++) { if (($x + $y == ...
- PHP一个小函数
// function makeTemp($fileName="index",$ftype=0) { $tempPath="xx/xxxx/{$fileName}.htm ...
- 用U盘制作并安装WIN10 64位原版系统的详细教程(该方法应该适用于任何一版的原版操作系统)
https://www.cnblogs.com/Jerseyblog/p/6518273.html
- [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...