class Solution {
public:
int divide(int dividend, int divisor) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
long long a = dividend;
long long b = divisor;
bool isNegative = false;
if (a < 0)
{
a = -a;
isNegative = !isNegative;
}
if (b < 0)
{
b = -b;
isNegative = !isNegative;
}
int d = 0;
while (b << d <= a)
{
++d;
}
--d;
int res = 0;
for (int i = d; i >= 0; --i)
{
if (b << i <= a)
{
res |= 1 << i;
a -= b << i;
}
}
if (isNegative)
res = -res;
return res;
}
};
[LeetCode] Divide Two Integers
最新推荐文章于 2021-05-07 19:18:17 发布