算法练习--LeetCode--29. Divide Two Integers
- 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: 3Example 2:
Input: dividend = 7, divisor = -3
Output: -2Note:
- 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.
简单来说就是不用‘乘法’、‘除法’和‘取余’运算来求两个整数的商,注意结果要在 [−231, 231 − 1]
Round One
暴力减法(如下),我赌5毛,超时(Time Limit Exceeded)!
- // Swift Code
- class Solution {
- func divide(_ dividend: Int, _ divisor: Int) -> Int {
- let sign = (dividend >= 0) == (divisor > 0) ? 1 : -1
- var dividend = abs(dividend)
- let divisor = abs(divisor)
- var result = 0
- while dividend > divisor {
- dividend -= divisor
- result += 1
- }
- if dividend == divisor {
- result += 1
- }
- return sign < 0 ? -result : result
- }
- }
Round Two
一个一个减肯定是超时了,要是一批一批减呢?
所以就需要先成倍放大被除数,不允许用‘乘法’、‘除法’和‘取余’ 还有 ‘<<’、‘>>’
这个方法耗时少于超越了100%的其它Swift提交
- // Swift Code
- class Solution {
- func divide(_ dividend: Int, _ divisor: Int) -> Int {
- // 除数、被除数符号不一致时候商为负数
- let sign = (dividend >= 0) == (divisor > 0) ? 1 : -1
- // 扩大下数据类型,避免溢出
- var _dividend = Int64(abs(dividend))
- let _divisor = Int64(abs(divisor))
- var result = 0
- var temp = 1
- var _divisor_temp = _divisor
- // 放大被除数
- while _divisor_temp < _dividend {
- _divisor_temp = _divisor_temp << 1
- temp = temp << 1
- }
- // 在合理范围内缩小被放大的被除数
- while _divisor_temp > 0, _divisor_temp > _divisor {
- while _divisor_temp > _dividend {
- _divisor_temp = _divisor_temp >> 1
- temp = temp >> 1
- }
- _dividend -= _divisor_temp
- result += temp
- }
- // 竟然一样大,所以再来一次了
- if _dividend == _divisor {
- result += 1
- }
- // 结果是有范围限制的
- return sign < 0 ? max(-result, Int(Int32.min)) : min(result, Int(Int32.max))
- }
- }
TestCase
- // Swift Code
- assert(Solution().divide(10, 3) == 3)
- assert(Solution().divide(3, 3) == 1)
- assert(Solution().divide(1, 1) == 1)
- assert(Solution().divide(2, 3) == 0)
- assert(Solution().divide(7, -3) == -2)
- assert(Solution().divide(-2147483648, -1) == 2147483647)
- assert(Solution().divide(0, 2147483648) == 0)
算法练习--LeetCode--29. Divide Two Integers的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. 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 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- 23. 客户默认选项(Default Customer Options)
Editing Email Templates Email Sender Contact Us
- [React] Persist Form Data in React and Formik with formik-persist
It can be incredibly frustrating to spend a few minutes filling out a form only to accidentally lose ...
- linux系统之shell编程-正則表達式
shell编程正則表達式: 1:元字符 [ ] . * ? + ( ) | { } ^ $ 2 : [a-z0-9] 表示匹配随意数字和字母的一个 3 : [^a-z] ...
- go test test & benchmark
开发程序其中很重要的一点是测试,我们如何保证代码的质量,如何保证每个函数是可运行,运行结果是正确的,又如何保证写出来的代码性能是好的,我们知道单元测试的重点在于发现程序设计或实现的逻辑错误,使问题及早 ...
- android项目笔记(一)
1.getInstance:单例模式创建类的实例,getInstance在单例模式(保证一个类仅有一个实例,并提供一个访问它的全局访问点)的类中常见,用来生成唯一的实例,getInstance往往是s ...
- 鼠标滚轮实现图片的缩放-------Day79
今天是7月的最后一天了,不得不说,我定下的七月份剩余几天的计划是完不成了.一则工作确实紧了些,再则没能处理好生活.工作和学习的节奏.这才是人生最大的课题吧.只是也还好.至少自己还在坚持着.事实上真的越 ...
- nginx-伤心的事
今天测试接口,总是出问题,测试了两天,整个流程就卡在最后一步. 每次采用curl,request等请求访问我的服务器都会返回403状态码,网上找了很多资料 有权限的,有静态文件的,然而很多都没有什么卵 ...
- 微软开源 Try .NET - 创建交互式.NET文档
微软近日开源了一个新平台--Try .NET,该平台可以让开发者在线上编写并运行 .NET 代码.微软介绍,Try .NET 是一个可嵌入的代码运行器,不仅可以直接在线上对自己或者他人的代码进行编辑. ...
- ajax返回页面停留跳转
ajax返回数据后,页面停留跳转. 原理:利用匿名函数自动运行的特性和定时器来完成. (function(){ ; // 设置停留时间单位秒 var href =data.url; //设置跳转的ur ...
- Git使用之Permission Denied问题解决
今天碰到了Git的Permission Denied问题. 在安装好git之后,我们通常会配置username和邮箱 git config --global user.name "zengj ...