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. linux驱动开发流程

    嵌入式linux驱动开发流程嵌入式系统中,操作系统是通过各种驱动程序来驾驭硬件设备的.设备驱动程序是操作系统内核和硬件设备之间的接口,它为应用程序屏蔽了硬件的细节,这样在应用程序看来,硬件设备只是一个 ...

  2. function 之 arguments 、call 、apply

    1.arguments arguments.length为函数实参个数,arguments.callee引用函数自身. arguments他的特性和使用方法 特性: arguments对象和Funct ...

  3. LeetCode_3Sum

    一.题目 3Sum Total Accepted: 45112 Total Submissions: 267165My Submissions Given an array S of n intege ...

  4. The user's guide what comes in the kernel Documentation directory

    The Linux IPMI Driver --------------------- Corey Minyard <minyard@mvista.com> <minyard@acm ...

  5. Neutron配置Floating IP

    下图是由一个虚拟机vm1,一个路由器ext-router,两个网络ext-net及demo-net组成的拓扑结构.要达到的目的很简单,即vm1不仅能使用私网IP和内部的其它虚拟机进行通信,还可以通过e ...

  6. (转)我在北京工作这几年 – 一个软件工程师的反省

    我于2007年来到北京,在北京工作这些年,先后在NEC.风行.百度几家公司担任软件工程师的职务.NEC是一家具有百年历史的传统日企,在知春路的分公司叫日电电子,我们部门主要从事机顶盒.数字电视上嵌入式 ...

  7. ubuntu 系统启动时找不到根文件系统

    报出的错误大概为: Alert! /dev/disk/by-uuid/ ....... does not exist 最后进入了 initramdiskfs 的命令行终端 . 输入 blkid 命令却 ...

  8. hibernate工作原理及作用

    转载自 http://www.cnblogs.com/dashi/p/3597969.html#commentform JAVA Hibernate工作原理及为什么要用 hibernate 简介:hi ...

  9. jetty java文件无法删除 java文件占用 delete无效 运行时锁定静态资源的解决方法

    前几天jetty下发现java无法删除文件,文件操作后一直被jvm占用,无奈换了tomcat问题消失. 今天又想起来,尝试网上的解决方法,经本人试验,直接修改配置文件有时不能生效,具体原因不清楚,建议 ...

  10. gitbash使用

    gitbash是什么 git bash是Windows下的命令行工具. 基于msys GNU环境,有git分布式版本控制工具. 主要用于git版本控制,上传下载项目代码. GNU环境,就是说如果你喜欢 ...