算法练习--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 ...
随机推荐
- iOS10获得系统权限
iOS 10 对系统隐私权限的管理更加严格,如果你不设置就会直接崩溃,一般解决办法都是在info.plist文件添加对应的Key-Value就可以了. <!-- 相册 --> <ke ...
- C/C++二进制读写png文件
以下代码只有最简单的读写.地址定位啥的,个别注释中有.如果要改动png的格式甚么的就要再了解一下png的数据结构如果要十进制的话就跟着注释改一下: /*! * \file CC++二进制读写png文件 ...
- 【Hibernate】(2)Hibernate配置与session、transaction
1. Hibernate经常使用配置 使用hibernate.default_schema属性能够让全部生成的表都带一个指定的前缀. 2. session简单介绍 不建议直接使用jdbc的connec ...
- VS2013带来的"新特性"
VS2013除了引入"Bootstrap"库以外,最大的变化就是.net Framework 4.5下面的都不支持了.也就是说,假设不把.net库升级成.net Framework ...
- 常用shell命令的写法
这并不是教人怎么进行shell编程的文章,只是韦哥在工作中用到的一些简单脚本的写法.因为有些命令即使用过几次了,再次使用时仍然写不对,需要man来看下或者需要google,你也可以理解为对命令的理解不 ...
- 初解C#类、结构、弱引用
一.类 类中的数据和函数称为类的成员 数据成员是包含类的数据----字段.常量和事件的成员.数据成员可以是静态数据.类成员总是实例成员,除非用static进行显示的声明. 事件是类的成员,在发生某些行 ...
- HDOJ1004 数组还要自己初始化
#include <iostream> #include <stdio.h> #include "string.h"using namespace std; ...
- 【HDU 5384】Danganronpa(AC自己主动机)
看官方题解貌似就是个自己主动机裸题 比赛的时候用kuangbin的AC自己主动机模板瞎搞的,居然A了,并且跑的还不慢.. 存下模板吧 #include<cstdio> #include&l ...
- xamarin.Android 实现横向滚动导航
经过一段时间的练习,自己也做了不少的demo和一些小项目,今天就把这些demo分享给大家,也当做笔记记录到自己的博客中. 这次给大家带来的是HorizontalScrollView实现横向滑动,参考博 ...
- 使用URL dispatcher的范例
在上面的一篇文章中,我们介绍了怎样使用URL disptacher.在这篇文章中,我们来通过一个范例更进一步来了解怎样实现它. 1)创建一个具有URL dispatcher的应用 我们首先打开我们的S ...