1. 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.

简单来说就是不用‘乘法’、‘除法’和‘取余’运算来求两个整数的商,注意结果要在 [−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的更多相关文章

  1. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  2. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  3. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

  4. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  5. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. [LeetCode] 29. Divide Two Integers ☆☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  7. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  9. LeetCode: 29. Divide Two Integers (Medium)

    1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...

  10. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

随机推荐

  1. flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)

    函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...

  2. Invocation of destroy method failed on bean with name ‘XXXX’

    项目启动报错问题:Invocation of destroy method failed on bean with name 'scopedTarget.eurekaClient': org.spri ...

  3. UP Board USB无线网卡选购指南

    前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 kernel 4.4.0 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我 ...

  4. CMS - 认识目录

    Tips:如果网页图片(文字)看不清,请按CTRL+鼠标滚轮 一个完整的小程序模板目录结构如下! 本章节给出的建议: 1.推荐使用flex布局 2.其它图片路径建议引入网络路径(tabBar不支持网络 ...

  5. SAP 锁对象 基本概念与基本操作 SE11

      一.SAP为什么要设置锁:     1,保持数据的一致性     假设几个用户要訪问相同的资源,须要找到一种同步訪问的方法去保持数据的一致性.比方说,在航班预订系统中,须要检查还有没有空座位,当检 ...

  6. <bgsound> - 背景音乐

    摘要 项目 说明 形式 <bgsound src="..."> 支持 e2+ 标签省略 开始标签:必须,结束标签:无 ■ 说明 bgsound 是 background ...

  7. JS判断访问设备(userAgent)加载不同页面 JS判断客户端操作系统类型(platform)

    //平台.设备和操作系统 var system ={ win : false, mac : false, xll : false }; //检测平台 var p = navigator.platfor ...

  8. ViewPagerTransforms

    https://github.com/eltld/ViewPagerTransforms

  9. Alert提示框之后跳转指定页面

    <li onclick="closes();">BTC</li> alert跳转到指定页面 <script type="text/javas ...

  10. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...