LeetCode_29. 两数相除Divide Two Integers|商的二进制表示与除数的关系
Original Link: 29. Divide Two Integers
Problem description
Given two integers: dividend
and divisor
, return dividend/divisor
without using multiplication, division, and mod operator.
The integer division should truncate toward zero, which means just discard fractional part.
Example:
Input: dividend = 10, divisor = -3
Output: -3
Explanation: 10/(-3) = -3.3333..., which should be truncated to -3.
Notes:
The dealing with an environment which could only store integers within 32-bit signed integer range: .
If the quotient is greater than , return .
If the quotient is less than , return .
Problem analysis
Firstly, we should consider the sign of divisor and dividend, in order to ignore different sign, we can turn them to same sign first. In this question, the integer bound are 32-bit signed integers, so we can only turn the divisor and dividend into negative(otherwise turn to positive will overflow).
Now let's consider the division between two negative number.
We can easily come up with a naive solution: continually minus divisor to dividend till dividend become larger than divisor. In this process, we count how many divisor are subtracted.
But it's obviously inefficient, we can minus multiple divisor one time! Suppose , is 1011
in binary, which means:
We can find the smallest such that , then minus with , and continually minus till .
Code details
class Solution {
public int divide(int m, int n) {
// consider special situations
if (n == Integer.MIN_VALUE)
return m == Integer.MIN_VALUE ? 1 : 0;
if (m == Integer.MIN_VALUE) {
if (n == 1)
return m;
if (n == -1)
return Integer.MAX_VALUE;
}
if (m == 0)
return 0;
// turn m,n into negative
boolean rev = false;
if (m > 0) {
rev = !rev;
m = -m;
}
if (n > 0) {
rev = !rev;
n = -n;
}
// find the smallest n*(2^k) such that n*(2^k) > m
int nPow = n, cnt = 0;
// attention overflow: using - instead of nPow+nPow>=m
while (nPow >= m - nPow) {
nPow += nPow;
++cnt;
}
// continually minus binary pow times n
int res = 0;
while (m <= n) {
if (m <= nPow) {
res += (1 << cnt);
m -= nPow;
}
nPow >>= 1;
--cnt;
}
return rev ? -res : res;
}
}
Complexity analysis
Time Complexity
We are performing minus of binary powers times to ( ). So we can handle them in , while is in 32-bit range. So the time complexity is , in which is the range of 32-bit integer.
Space Complexity
Firstly we find the smallest such that , then we perform continually minus of to if , where . And we can easily get by . The total cost of space are constant, so it's .
ORIGINAL POST LINK——FROM CNBLOG.liuzhch1
https://www.cnblogs.com/liuzhch1/p/leetcode29-divide-two-integers.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架