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. 通过BSSID和无线流量传输后门Payload

    本文将探讨无线接入点(AP)和BSSID(MAC地址AP).我们不借助文件系统加密和文件系统中(仅内存中)的硬编码Payload即可获得后门Payload,通过该方法可绕过所有的杀软,可以不使用Pay ...

  2. 【Todo】UDP P2P打洞原理

    参考以下两篇文章: https://my.oschina.net/ososchina/blog/369206 http://m.blog.csdn.net/article/details?id=666 ...

  3. 碰撞检測之OBB-OBB的SweepTest

    提要 当物体在运动的时候.普通的每帧进行碰撞检測已经无法满足要求,比方子弹的运动 两帧的位置已经直接将中间的板子穿过了,所以 t 时刻和 t +1 时刻的检測都是失效的.这时候须要用到的就是sweep ...

  4. go使用时间作为种子生成随机数

    原文:http://blog.csdn.net/qq_15437667/article/details/50851159 --------------------------------------- ...

  5. Netty3 源代码分析 - NIO server绑定过程分析

    Netty3 源代码分析 - NIO server绑定过程分析      一个框架封装的越好,越利于我们高速的coding.可是却掩盖了非常多的细节和原理.可是源代码可以揭示一切. 服务器端代码在指定 ...

  6. IOS UIPickView+sqlite 选择中国全部城市案例

    1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...

  7. react map 遍历

    1.map方法 注:map 返回的是一个新数组 class App extends Component { // constructor(props) { // super(props); // th ...

  8. SVN系列之—-SVN版本回滚的办法

    例:SVN版本为:TortoiseSVN 1.9.7 一.SVN简介 subversion(简称svn)是一种跨平台的集中式版本控制工具,支持linux和windows. 版本控制解决了:*代码管理混 ...

  9. Android Studio代码自己主动检測错误提示

    Android Studio的代码自己主动检測的错误提示方式感觉有点奇葩.和Eclipse区别非常大,Eclipse检測到某个资源文件找不到或者错误,都会在Project中相应的文件前面打叉.可是An ...

  10. CA与数字证书的自结

    1.CA CA(Certificate Authority)是数字证书认证中心的简称,是指发放数字证书.管理数字证书.废除数字证书的权威机构. 2.数字证书 如果向CA申请数字证书的单位为A.则他申请 ...