当前位置:网站首页>【leetcode】29. Divide two numbers
【leetcode】29. Divide two numbers
2022-07-04 09:16:00 【Chinese fir sauce_】
subject :
29. Divide two numbers
Given two integers , Divisor dividend And divisor divisor. Divide two numbers , Multiplication is not required 、 Division and mod Operator .
Return dividend dividend Divide by divisor divisor Get the business .
The result of integer division should be truncated (truncate) Its decimal part , for example :truncate(8.345) = 8 as well as truncate(-2.7335) = -2
Example 1:
Input : dividend = 10, divisor = 3
Output : 3
explain : 10/3 = truncate(3.33333..) = truncate(3) = 3
Example 2:
Input : dividend = 7, divisor = -3
Output : -2
explain : 7/-3 = truncate(-2.33333..) = -2
Tips :
Both dividend and divisor are 32 Bit signed integer .
The divisor is not 0.
Suppose our environment can only store 32 Bit signed integer , The value range is [−231, 231 − 1]. In this question , If the division result overflows , Then return to 231 − 1.
class Solution {
/** * Divide two numbers * * @param dividend Divisor * @param divisor Divisor * @return merchant ( Do not include decimals ) */
public static int divide(int dividend, int divisor) {
long result = 0;
long x = dividend;
long y = divisor;
boolean negative = (x < 0 && y > 0) || (x > 0 && y < 0);
if (x < 0) {
x = -x;
}
if (y < 0) {
y = -y;
}
// because x/y The result must be [0,x] Within the scope of , So for x Use dichotomy
long left = 0;
long right = x;
while (left < right) {
long mid = left + right + 1 >> 1;
if (quickMulti(mid, y) <= x) {
// The multiplication result is not greater than x, Move the left pointer to the right
left = mid;
} else {
right = mid - 1;
}
}
result = negative ? -left : left;
// Judge whether it overflows
if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int)result;
}
/** * Fast multiplication * * @param a The multiplier * @param b Multiplier * @return product */
public static long quickMulti(long a, long b) {
long result = 0;
while (b > 0) {
if ((b & 1) == 1) {
// The current minimum is 1, Add in the result a
result += a;
}
// The multiplicand moves to the right 1 position , Equivalent to divided by 2
b >>= 1;
// Multiplier multiplication , Equivalent to times 2
a += a;
}
return result;
}
}
边栏推荐
- 2022-2028 global special starch industry research and trend analysis report
- 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
- MySQL foundation 02 - installing MySQL in non docker version
- 2022-2028 global visual quality analyzer industry research and trend analysis report
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
- Awk from entry to earth (14) awk output redirection
- Codeforces Round #793 (Div. 2)(A-D)
- 【无标题】转发最小二乘法
- Horizon sunrise X3 PI (I) first boot details
猜你喜欢
[C Advanced] file operation (2)
AMLOGIC gsensor debugging
2022-2028 global intelligent interactive tablet industry research and trend analysis report
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
2022-2028 global optical transparency industry research and trend analysis report
awk从入门到入土(12)awk也可以写脚本,替代shell
GoLand environment variable configuration
Turn: excellent managers focus not on mistakes, but on advantages
Live in a dream, only do things you don't say
26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
随机推荐
Reading notes on how to connect the network - hubs, routers and routers (III)
There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
Opencv environment construction (I)
After unplugging the network cable, does the original TCP connection still exist?
Markdown syntax
2022-2028 global tensile strain sensor industry research and trend analysis report
Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
Service call feign of "micro service"
Leetcode topic [array] - 121 - the best time to buy and sell stocks
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
Solve the problem of "Chinese garbled MySQL fields"
How to write unit test cases
Awk from entry to earth (12) awk can also write scripts to replace the shell
2022-2028 global visual quality analyzer industry research and trend analysis report
Sequence model
awk从入门到入土(4)用户自定义变量
Awk from entry to earth (15) awk executes external commands
Awk from entry to earth (7) conditional statements
The old-fashioned synchronized lock optimization will make it clear to you at once!
Awk from getting started to digging in (11) detailed explanation of awk getline function