Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
divide-two-integers.cpp 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-07-11 22:06 +08:00 . Rename divide.cpp to divide-two-integers.cpp
// Time: O(logn) = O(1)
// Space: O(1)
// Only using integer type.
class Solution {
public:
int divide(int dividend, int divisor) {
// Handle corner case.
if (dividend == numeric_limits<int>::min() && divisor == -1) {
return numeric_limits<int>::max();
}
int a = dividend > 0 ? -dividend : dividend;
int b = divisor > 0 ? -divisor : divisor;
int shift = 0;
while (b << shift < 0 && shift < 32) {
++shift;
}
shift -= 1;
int result = 0;
while (shift >= 0) {
if (a <= b << shift) {
a -= b << shift;
result += 1 << shift;
}
--shift;
}
result = (dividend ^ divisor) >> 31 ? -result : result;
return result;
}
};
// Time: O(logn) = O(1)
// Space: O(1)
// Using long long type.
class Solution2 {
public:
int divide(int dividend, int divisor) {
long long result = 0;
long long a = llabs(dividend);
long long b = llabs(divisor);
int shift = 31;
while (shift >= 0) {
if (a >= b << shift) {
a -= b << shift;
result += 1LL << shift;
}
--shift;
}
result = ((dividend ^ divisor) >> 31) ? -result : result;
return min(result, static_cast<long long>(numeric_limits<int>::max()));
}
};
// Time: O(logn) = O(1)
// Space: O(1)
// a / b = exp^(ln(a) - ln(b))
class Solution3 {
public:
int divide(int dividend, int divisor) {
if (dividend == 0) {
return 0;
}
if (divisor == 0) {
return numeric_limits<int>::max();
}
long long result = exp(log(fabs(dividend)) - log(fabs(divisor)));
result = ((dividend ^ divisor) >> 31) ? -result : result;
return min(result, static_cast<long long>(numeric_limits<int>::max()));
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助